Added support for number literals and punctuation.

This commit is contained in:
2025-01-08 01:39:10 -06:00
parent 6039918707
commit 6d3f90183e
4 changed files with 64 additions and 3 deletions
+2
View File
@@ -1,2 +1,4 @@
bin/
obj/
.vscode/ .vscode/
tags tags
+1 -1
View File
@@ -34,7 +34,7 @@ int main(int argc, char** argv) {
for(int i = 0; i < tokens->Size; i++) { for(int i = 0; i < tokens->Size; i++) {
Token* token = tokens->Items[i]; Token* token = tokens->Items[i];
printf("%s ", token->Value.String);//TokenStringify(token, false)); printf("%s ", TokenStringify(token, false));
} }
printf("\n"); printf("\n");
+1
View File
@@ -44,6 +44,7 @@ char* TokenStringify(const struct _token* token, bool valueOnly) {
snprintf(result, length + 1, "%s ", type); snprintf(result, length + 1, "%s ", type);
break; break;
default: default:
snprintf(result, length + 2, "%s %c", type, token->Value.Operator[0]);
//snprintf(buffer, length, "<%c>", type); //snprintf(buffer, length, "<%c>", type);
break; break;
} }
+60 -2
View File
@@ -13,8 +13,8 @@ Array* Tokens;
const char* SourceCode; const char* SourceCode;
size_t SourceCodeLength = 0; size_t SourceCodeLength = 0;
size_t SourceCodeIndex = 0; size_t SourceCodeIndex = 0;
int SourceLineNumber = 0; int SourceLineNumber = 1;
int SourceColumnNumber = 0; int SourceColumnNumber = 1;
char PeekTokenizer(void); char PeekTokenizer(void);
bool TryPopTokenizer(char* c); bool TryPopTokenizer(char* c);
@@ -24,6 +24,7 @@ bool BackTokenzier(void);
void TokenizerExpectWord(void); void TokenizerExpectWord(void);
void TokenizerExpectString(void); void TokenizerExpectString(void);
void TokenizerExpectNumber(void);
void TokenizerIgnoreLine(void); void TokenizerIgnoreLine(void);
Array* Tokenize(const char* sourceCode, Dictionary** variables) { Array* Tokenize(const char* sourceCode, Dictionary** variables) {
@@ -43,6 +44,12 @@ Array* Tokenize(const char* sourceCode, Dictionary** variables) {
continue; continue;
} }
if (isdigit(c)) {
TokenizerExpectNumber();
continue;
}
if (c == '"') { if (c == '"') {
AdvanceTokenizer(); AdvanceTokenizer();
@@ -57,6 +64,27 @@ Array* Tokenize(const char* sourceCode, Dictionary** variables) {
continue; continue;
} }
switch(c) {
case '\n':
case '+':
case '-':
case '*':
case '/':
case '^':
case '(':
case ')':
case '[':
case ']':
Token* token = TokenCreate((TokenType)c, SourceLineNumber, SourceColumnNumber, true);
token->Value.Operator[0] = c;
ArrayAdd(Tokens, token);
break;
default:
break;
}
AdvanceTokenizer(); AdvanceTokenizer();
} }
@@ -65,6 +93,28 @@ Array* Tokenize(const char* sourceCode, Dictionary** variables) {
return Tokens; return Tokens;
} }
void TokenizerExpectNumber(void) {
char word[256] = { 0 };
int wordLength = 0;
char c;
while(TryPopTokenizer(&c)) {
if (wordLength == 255) break; //Sync, error etc here at some point.
word[wordLength] = c;
wordLength++;
}
if (wordLength > 0) {
Token* token = TokenCreate(Number, SourceLineNumber, SourceColumnNumber, false);
token->Value.Number = (int) strtol(word, NULL, 0);
ArrayAdd(Tokens, token);
}
}
void TokenizerExpectWord() { void TokenizerExpectWord() {
char word[256] = { 0 }; char word[256] = { 0 };
int wordLength = 0; int wordLength = 0;
@@ -171,6 +221,13 @@ char AdvanceTokenizer() {
char c = SourceCode[SourceCodeIndex]; char c = SourceCode[SourceCodeIndex];
if (c == '\n')
{
SourceColumnNumber = 1;
SourceLineNumber++;
}
SourceColumnNumber++;
SourceCodeIndex++; SourceCodeIndex++;
return c; return c;
@@ -179,6 +236,7 @@ char AdvanceTokenizer() {
bool BackTokenzier(void) { bool BackTokenzier(void) {
if (SourceCodeIndex == 0) return false; if (SourceCodeIndex == 0) return false;
SourceColumnNumber--;
SourceCodeIndex--; SourceCodeIndex--;
return true; return true;