update
This commit is contained in:
+53
-12
@@ -9,27 +9,39 @@ int start(char* src) {
|
||||
return _STOP_; // stopping the state machine in any other case
|
||||
}
|
||||
|
||||
int getNextCharacter(char* pos) {
|
||||
if (isLetter(*pos)) {
|
||||
return _READ_IDENTIFIER_; // continues to read the identifier
|
||||
}
|
||||
int getNextCharacter(char** ptr_current) {
|
||||
char* pos = *ptr_current;
|
||||
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
|
||||
}
|
||||
|
||||
int readIdentifier(char* src) {
|
||||
return _STOP_; // placeholder
|
||||
int readIdentifier(char** ptr_current) {
|
||||
char* pos = *ptr_current;
|
||||
printf("%c - Identifier\n", *pos);
|
||||
pos++;
|
||||
*ptr_current = pos;
|
||||
return _GET_NEXT_CHARACTER_;
|
||||
}
|
||||
|
||||
int readNumLit(char* src) {
|
||||
return _STOP_; // placeholder
|
||||
int readNumLit(char** ptr_current) {
|
||||
char* pos = *ptr_current;
|
||||
printf("%c - Number\n", *pos);
|
||||
return _GET_NEXT_CHARACTER_;
|
||||
}
|
||||
|
||||
int readOperator(char* src) {
|
||||
return _STOP_; // placeholder
|
||||
int readOperator(char** ptr_current) {
|
||||
char* pos = *ptr_current;
|
||||
printf("%c - Operator\n", *pos);
|
||||
return _GET_NEXT_CHARACTER_;
|
||||
}
|
||||
|
||||
int readPunctuation(char* src) {
|
||||
return _STOP_; // placeholder
|
||||
int readPunctuation(char** ptr_current) {
|
||||
char* pos = *ptr_current;
|
||||
printf("%c - Punctuation\n", *pos);
|
||||
return _GET_NEXT_CHARACTER_;
|
||||
}
|
||||
|
||||
int isLetter(char in) {
|
||||
@@ -45,3 +57,32 @@ int isLetter(char in) {
|
||||
}
|
||||
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
@@ -18,10 +18,14 @@
|
||||
|
||||
/* functions */
|
||||
int start(char*);
|
||||
int getNextCharacter(char*);
|
||||
int readIdentifier(char*);
|
||||
int readNumLit(char*);
|
||||
int readOperator(char*);
|
||||
int readPunctuation(char*);
|
||||
int getNextCharacter(char**);
|
||||
int readIdentifier(char**);
|
||||
int readNumLit(char**);
|
||||
int readOperator(char**);
|
||||
int readPunctuation(char**);
|
||||
int main();
|
||||
int isLetter(char);
|
||||
int isWhiteSpace(char);
|
||||
int isNum(char);
|
||||
int isOperator(char);
|
||||
int isPunctuation(char);
|
||||
|
||||
@@ -6,9 +6,11 @@ int main() {
|
||||
// variables
|
||||
char* sourcecode = "index = 2 * count + 42;"; // input string
|
||||
int lexer_state = _START_; // int var for the state machine
|
||||
char* current_pos = sourcecode;
|
||||
char** ptr_current = ¤t_pos;
|
||||
|
||||
// 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
|
||||
do {
|
||||
@@ -17,19 +19,19 @@ int main() {
|
||||
lexer_state = start(sourcecode);
|
||||
break;
|
||||
case _GET_NEXT_CHARACTER_:
|
||||
lexer_state = getNextCharacter(sourcecode);
|
||||
lexer_state = getNextCharacter(ptr_current);
|
||||
break;
|
||||
case _READ_IDENTIFIER_:
|
||||
lexer_state = readIdentifier(sourcecode);
|
||||
lexer_state = readIdentifier(ptr_current);
|
||||
break;
|
||||
case _READ_NUM_LIT_:
|
||||
lexer_state = readNumLit(sourcecode);
|
||||
lexer_state = readNumLit(ptr_current);
|
||||
break;
|
||||
case _READ_OPERATOR_:
|
||||
lexer_state = readOperator(sourcecode);
|
||||
lexer_state = readOperator(ptr_current);
|
||||
break;
|
||||
case _READ_PUNCTUATION_:
|
||||
lexer_state = readPunctuation(sourcecode);
|
||||
lexer_state = readPunctuation(ptr_current);
|
||||
break;
|
||||
default: lexer_state = _ERROR_;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user