Compare commits

..

No commits in common. "bd8215fd73f86289cfb6dbf1a57258bae52b0e32" and "1729070b80e0a128202db0bd9b367a5269f05d2f" have entirely different histories.

3 changed files with 15 additions and 104 deletions

View File

@ -162,8 +162,7 @@ static void readLabel(Compiler* cmp){
return; return;
} }
if(!isAlphabeticalLower(c) && !isAlphabeticalUpper(c) && !isDigit(c) && if(!isAlphabeticalLower(c) && !isAlphabeticalUpper(c) && !isDigit(c)){
c != '_' && c != '.'){
setError(Error_unexpectedCharacter(c)); setError(Error_unexpectedCharacter(c));
return; return;
} }
@ -188,7 +187,7 @@ static void readArguments(Compiler* cmp){
while(cmp->pos < cmp->code_len){ while(cmp->pos < cmp->code_len){
c = cmp->code[cmp->pos]; c = cmp->code[cmp->pos];
// end of line // end of line
if(c == '\r' || c == '\n' || c == ';'){ if(c == '\r' || c == '\n'){
tok.length = cmp->pos - tok.begin; tok.length = cmp->pos - tok.begin;
if(tok.length > 0) if(tok.length > 0)
List_Token_push(&cmp->tokens, tok); List_Token_push(&cmp->tokens, tok);
@ -223,7 +222,7 @@ static void readInstruction(Compiler* cmp){
while(cmp->pos < cmp->code_len){ while(cmp->pos < cmp->code_len){
char c = cmp->code[cmp->pos]; char c = cmp->code[cmp->pos];
// end of line // end of line
if(c == '\r' || c == '\n' || c == ';'){ if(c == '\r' || c == '\n'){
tok.length = cmp->pos - tok.begin; tok.length = cmp->pos - tok.begin;
List_Token_push(&cmp->tokens, tok); List_Token_push(&cmp->tokens, tok);
// cmp->line will be increased in lex() // cmp->line will be increased in lex()
@ -252,83 +251,6 @@ static void readInstruction(Compiler* cmp){
List_Token_push(&cmp->tokens, tok); List_Token_push(&cmp->tokens, tok);
} }
static void readChar(Compiler* cmp){
Token tok = Token_construct(TokenType_Char, cmp->pos, 0);
cmp->pos++;
cmp->column++;
while(cmp->pos < cmp->code_len){
char c = cmp->code[cmp->pos];
// end of line
if(c == '\r' || c == '\n'){
setError(Error_unexpectedCharacter(cmp->code[--cmp->pos]));
return;
}
if(c == '\''){
tok.length = cmp->pos - tok.begin + 1;
List_Token_push(&cmp->tokens, tok);
return;
}
cmp->column++;
cmp->pos++;
}
// end of file
setError(Error_endOfFile);
}
static void readString(Compiler* cmp){
Token tok = Token_construct(TokenType_String, cmp->pos, 0);
cmp->pos++;
cmp->column++;
while(cmp->pos < cmp->code_len){
char c = cmp->code[cmp->pos];
// end of line
if(c == '\r' || c == '\n'){
setError(Error_unexpectedCharacter(cmp->code[--cmp->pos]));
return;
}
if(c == '"'){
tok.length = cmp->pos - tok.begin + 1;
List_Token_push(&cmp->tokens, tok);
return;
}
cmp->column++;
cmp->pos++;
}
// end of file
setError(Error_endOfFile);
}
static void readNumber(Compiler* cmp){
Token tok = Token_construct(TokenType_Number, cmp->pos, 0);
cmp->pos++;
cmp->column++;
while(cmp->pos < cmp->code_len){
char c = cmp->code[cmp->pos];
if(c == '\r' || c == '\n' || c == ' ' || c == '\t' || c == ',' || c == ';'){
tok.length = cmp->pos - tok.begin;
List_Token_push(&cmp->tokens, tok);
return;
}
cmp->column++;
cmp->pos++;
}
// end of file
tok.length = cmp->pos - tok.begin;
List_Token_push(&cmp->tokens, tok);
}
static bool lex(Compiler* cmp){ static bool lex(Compiler* cmp){
returnErrorIf_auto(cmp->state != CompilerState_Initial); returnErrorIf_auto(cmp->state != CompilerState_Initial);
cmp->state = CompilerState_Lexing; cmp->state = CompilerState_Lexing;
@ -348,21 +270,14 @@ static bool lex(Compiler* cmp){
case '.': case '.':
readLabel(cmp); readLabel(cmp);
break; break;
case '"':
readString(cmp);
break;
case '\'':
readChar(cmp);
break;
default: default:
// try read instruction // try read instruction
if(isAlphabeticalLower(c) || isAlphabeticalUpper(c)) if(isAlphabeticalLower(c) || isAlphabeticalUpper(c)){
readInstruction(cmp); readInstruction(cmp);
else if(isDigit(c))
readNumber(cmp);
else returnError(Error_unexpectedCharacter(c));
break; break;
} }
else returnError(Error_unexpectedCharacter(c));
}
if(cmp->state == CompilerState_Error) if(cmp->state == CompilerState_Error)
return false; return false;
@ -443,8 +358,9 @@ bool Compiler_compileTasm(Compiler* cmp, cstr source_file_name, cstr out_file_na
char* tokstr = malloc(4096); char* tokstr = malloc(4096);
strncpy(tokstr, cmp->code + t.begin, t.length); strncpy(tokstr, cmp->code + t.begin, t.length);
tokstr[t.length] = 0; tokstr[t.length] = 0;
printf("[l:%3u, c:%3u] %s '%s'\n", printf("[l:%u, c:%u](pos:%u, size:%u) %s '%s'\n",
pos.line, pos.column, pos.line, pos.column,
t.begin, t.length,
TokenType_toString(t.type), tokstr); TokenType_toString(t.type), tokstr);
free(tokstr); free(tokstr);
} }

View File

@ -1,19 +1,15 @@
#include "token.h" #include "token.h"
static cstr _TokenType_str[] = { static cstr TokenType_str[] = {
"Unset", "Unset",
"SingleLineComment", "SingleLineComment",
"MultiLineComment", "MultiLineComment",
"Label", "Label",
"Instruction", "Instruction",
"Argument", "Argument",
"Number", "Data",
"Char",
"String",
}; };
cstr TokenType_toString(TokenType t){ cstr TokenType_toString(TokenType t){
if(t >= sizeof(_TokenType_str) / sizeof(cstr)) return TokenType_str[t];
return "!!INDEX_ERROR!!";
return _TokenType_str[t];
} }

View File

@ -8,17 +8,16 @@ typedef enum TokenType {
TokenType_Label, TokenType_Label,
TokenType_Instruction, TokenType_Instruction,
TokenType_Argument, TokenType_Argument,
TokenType_Number, TokenType_Data,
TokenType_Char, /* there is a place for 2 values left (TokenType must occupy 4 bits) */
TokenType_String,
} TokenType; } TokenType;
cstr TokenType_toString(TokenType t); cstr TokenType_toString(TokenType t);
typedef struct Token { typedef struct Token {
u32 begin; // some index in Compiler->code u32 begin; // some index in Compiler->code
u32 length : 24; // length in characters (24 bits) u32 length : 28; // length in characters (28 bits)
TokenType type : 8; // type of token (8 bits) TokenType type : 4; // type of token (4 bits)
} Token; } Token;
#define Token_construct(TYPE, BEGIN, END) ((Token){ .type = TYPE, .begin = BEGIN, .length = END }) #define Token_construct(TYPE, BEGIN, END) ((Token){ .type = TYPE, .begin = BEGIN, .length = END })