This commit is contained in:
SinusFox
2023-05-30 10:13:47 +02:00
parent 73510cb48f
commit a9baf4f223
3 changed files with 70 additions and 23 deletions
+53 -12
View File
@@ -9,27 +9,39 @@ int start(char* src) {
return _STOP_; // stopping the state machine in any other case return _STOP_; // stopping the state machine in any other case
} }
int getNextCharacter(char* pos) { int getNextCharacter(char** ptr_current) {
if (isLetter(*pos)) { char* pos = *ptr_current;
return _READ_IDENTIFIER_; // continues to read the identifier while(isWhiteSpace(*pos)) pos++;
} if (isLetter(*pos)) return _READ_IDENTIFIER_;
if (isNum(*pos)) return _READ_NUM_LIT_;
if (isOperator(*pos)) return _READ_OPERATOR_;
return _STOP_; // stopping the state machine in any other case return _STOP_; // stopping the state machine in any other case
} }
int readIdentifier(char* src) { int readIdentifier(char** ptr_current) {
return _STOP_; // placeholder char* pos = *ptr_current;
printf("%c - Identifier\n", *pos);
pos++;
*ptr_current = pos;
return _GET_NEXT_CHARACTER_;
} }
int readNumLit(char* src) { int readNumLit(char** ptr_current) {
return _STOP_; // placeholder char* pos = *ptr_current;
printf("%c - Number\n", *pos);
return _GET_NEXT_CHARACTER_;
} }
int readOperator(char* src) { int readOperator(char** ptr_current) {
return _STOP_; // placeholder char* pos = *ptr_current;
printf("%c - Operator\n", *pos);
return _GET_NEXT_CHARACTER_;
} }
int readPunctuation(char* src) { int readPunctuation(char** ptr_current) {
return _STOP_; // placeholder char* pos = *ptr_current;
printf("%c - Punctuation\n", *pos);
return _GET_NEXT_CHARACTER_;
} }
int isLetter(char in) { int isLetter(char in) {
@@ -45,3 +57,32 @@ int isLetter(char in) {
} }
return 0; // returning 0 if it's no letter return 0; // returning 0 if it's no letter
} }
int isWhiteSpace(char in) {
if (in == ' ' || in == '\n') return 1;
return 0;
}
int isNum(char in) {
if (in >= '0' && in <= 0) return 1;
return 0;
}
int isOperator(char in) {
switch(in) {
case '=':
break;
case '*':
break;
case '+':
break;
default:
return 0;
}
return 1;
}
int isPunctuation(char in) {
if (in == ';') return 1;
return 0;
}
+9 -5
View File
@@ -18,10 +18,14 @@
/* functions */ /* functions */
int start(char*); int start(char*);
int getNextCharacter(char*); int getNextCharacter(char**);
int readIdentifier(char*); int readIdentifier(char**);
int readNumLit(char*); int readNumLit(char**);
int readOperator(char*); int readOperator(char**);
int readPunctuation(char*); int readPunctuation(char**);
int main(); int main();
int isLetter(char); int isLetter(char);
int isWhiteSpace(char);
int isNum(char);
int isOperator(char);
int isPunctuation(char);
+8 -6
View File
@@ -6,9 +6,11 @@ int main() {
// variables // variables
char* sourcecode = "index = 2 * count + 42;"; // input string char* sourcecode = "index = 2 * count + 42;"; // input string
int lexer_state = _START_; // int var for the state machine int lexer_state = _START_; // int var for the state machine
char* current_pos = sourcecode;
char** ptr_current = &current_pos;
// checking var values // checking var values
printf(sourcecode); // printing the input string to check it printf("%c\n", sourcecode); // printing the input string to check it
// main loop // main loop
do { do {
@@ -17,19 +19,19 @@ int main() {
lexer_state = start(sourcecode); lexer_state = start(sourcecode);
break; break;
case _GET_NEXT_CHARACTER_: case _GET_NEXT_CHARACTER_:
lexer_state = getNextCharacter(sourcecode); lexer_state = getNextCharacter(ptr_current);
break; break;
case _READ_IDENTIFIER_: case _READ_IDENTIFIER_:
lexer_state = readIdentifier(sourcecode); lexer_state = readIdentifier(ptr_current);
break; break;
case _READ_NUM_LIT_: case _READ_NUM_LIT_:
lexer_state = readNumLit(sourcecode); lexer_state = readNumLit(ptr_current);
break; break;
case _READ_OPERATOR_: case _READ_OPERATOR_:
lexer_state = readOperator(sourcecode); lexer_state = readOperator(ptr_current);
break; break;
case _READ_PUNCTUATION_: case _READ_PUNCTUATION_:
lexer_state = readPunctuation(sourcecode); lexer_state = readPunctuation(ptr_current);
break; break;
default: lexer_state = _ERROR_; default: lexer_state = _ERROR_;
} }