Changes were made.
This commit is contained in:
parent
a6dc46149c
commit
b43fd46260
39
src/lexer.c
39
src/lexer.c
@ -1,10 +1,10 @@
|
|||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
#include <limits.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <limits.h>
|
|
||||||
|
|
||||||
#include "include/lexer.h"
|
|
||||||
#include "include/dstr.h"
|
#include "include/dstr.h"
|
||||||
|
#include "include/lexer.h"
|
||||||
#include "include/token.h"
|
#include "include/token.h"
|
||||||
#include "include/util.h"
|
#include "include/util.h"
|
||||||
|
|
||||||
@ -26,7 +26,8 @@ void lexer_init(char* src) {
|
|||||||
|
|
||||||
void lexer_destroy() {
|
void lexer_destroy() {
|
||||||
// Does not free lexer->src.
|
// Does not free lexer->src.
|
||||||
for (int i = 0; i < thelexer->ntokens; i++) token_destroy(thelexer->tokens[i]);
|
for (int i = 0; i < thelexer->ntokens; i++)
|
||||||
|
token_destroy(thelexer->tokens[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
void lexer_lex() {
|
void lexer_lex() {
|
||||||
@ -86,8 +87,8 @@ void lexer_do_call() {
|
|||||||
// Where the call string starts.
|
// Where the call string starts.
|
||||||
char* start = thelexer->cchar;
|
char* start = thelexer->cchar;
|
||||||
|
|
||||||
for (callln = 0;
|
for (callln = 0; *thelexer->cchar &&
|
||||||
*thelexer->cchar && (!isdigit(*thelexer->cchar) && !isspace(*thelexer->cchar));
|
(!isdigit(*thelexer->cchar) && !isspace(*thelexer->cchar));
|
||||||
callln++)
|
callln++)
|
||||||
lexer_inc();
|
lexer_inc();
|
||||||
|
|
||||||
@ -100,9 +101,7 @@ void lexer_do_call() {
|
|||||||
thelexer->state = LEXER_STATE_CONFUSED;
|
thelexer->state = LEXER_STATE_CONFUSED;
|
||||||
}
|
}
|
||||||
|
|
||||||
void lexer_inc() {
|
void lexer_inc() { thelexer->cchar += sizeof(char); }
|
||||||
thelexer->cchar += sizeof(char);
|
|
||||||
}
|
|
||||||
|
|
||||||
void lexer_add_token(Token* token) {
|
void lexer_add_token(Token* token) {
|
||||||
assert(thelexer->ntokens < TOKENS_MAX);
|
assert(thelexer->ntokens < TOKENS_MAX);
|
||||||
@ -121,13 +120,14 @@ void lexer_print_i(int ilvl) {
|
|||||||
INDENT_BEGIN(ilvl);
|
INDENT_BEGIN(ilvl);
|
||||||
INDENT_TITLE("Lexer", thelexer);
|
INDENT_TITLE("Lexer", thelexer);
|
||||||
INDENT_FIELD_NONL_START("state")
|
INDENT_FIELD_NONL_START("state")
|
||||||
lexerstate_print_raw();
|
lexerstate_print_raw();
|
||||||
INDENT_FIELD_NONL_END
|
INDENT_FIELD_NONL_END
|
||||||
INDENT_FIELD("srcln", "%ld", thelexer->srcln);
|
INDENT_FIELD("srcln", "%ld", thelexer->srcln);
|
||||||
INDENT_FIELD_NL("src", "\"%s\"", thelexer->src);
|
INDENT_FIELD_NL("src", "\"%s\"", thelexer->src);
|
||||||
INDENT_FIELD("cchar", "'%c'", *thelexer->cchar);
|
INDENT_FIELD("cchar", "'%c'", *thelexer->cchar);
|
||||||
INDENT_FIELD("ntokens", "%ld", thelexer->ntokens);
|
INDENT_FIELD("ntokens", "%ld", thelexer->ntokens);
|
||||||
INDENT_FIELD_LIST("tokens", thelexer->tokens, thelexer->ntokens, token_print_i);
|
INDENT_FIELD_LIST("tokens", thelexer->tokens, thelexer->ntokens,
|
||||||
|
token_print_i);
|
||||||
}
|
}
|
||||||
|
|
||||||
void lexerstate_print_raw() {
|
void lexerstate_print_raw() {
|
||||||
@ -140,31 +140,32 @@ void lexerstate_print_raw() {
|
|||||||
|
|
||||||
#include "../build/grammars/grammar.tab.h"
|
#include "../build/grammars/grammar.tab.h"
|
||||||
|
|
||||||
|
extern YYSTYPE yylval;
|
||||||
|
|
||||||
int yylex(void) {
|
int yylex(void) {
|
||||||
if (*thelexer->cchar == '\0') return YYEOF;
|
if (*thelexer->cchar == '\0') return YYEOF;
|
||||||
|
|
||||||
switch (*thelexer->cchar) {
|
switch (*thelexer->cchar) {
|
||||||
case ' ':
|
case ' ':
|
||||||
case '\t':
|
case '\t': thelexer->cchar++;
|
||||||
thelexer->cchar++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Assign & consume current character.
|
// Assign & consume current character.
|
||||||
int c = *thelexer->cchar++;
|
int c = *thelexer->cchar++;
|
||||||
|
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case '+':
|
case '+': return PLUS;
|
||||||
return PLUS;
|
default: return CALL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isdigit(c)) {
|
if (isdigit(c)) {
|
||||||
int value = c - '0'; // Start with the first digit
|
int value = c - '0'; // Start with the first digit
|
||||||
while (isdigit(*thelexer->cchar)) {
|
while (isdigit(*thelexer->cchar)) {
|
||||||
value = value * 10 + (*thelexer->cchar - '0'); // Accumulate value
|
value = value * 10 + (*thelexer->cchar - '0'); // Accumulate value
|
||||||
thelexer++;
|
thelexer++;
|
||||||
}
|
}
|
||||||
yylval.intval = value; // Set the token value
|
yylval.intval = value; // Set the token value
|
||||||
return NUM; // Return the INTEGER token type
|
return NUM; // Return the INTEGER token type
|
||||||
}
|
}
|
||||||
|
|
||||||
fprintf(stderr, "Unexpected character: %c\n", c);
|
fprintf(stderr, "Unexpected character: %c\n", c);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user