Commit 53ae27be authored by Chris Müller's avatar Chris Müller
Browse files

Merge Float and Fixnum statemachines together

parent 9683cd38
......@@ -221,88 +221,6 @@ issymbol(int ch)
ch == '#' || ch == ':' || ch == '.' || ch == '~' || ch == '_';
}
enum float_state {
FP_START,
FP_EXPONENT,
FP_DOT,
FP_DECIMAL,
FP_MINUSPLUS,
FP_FINAL
};
static enum token
lex_float(byte_t** begin, byte_t* buffer, size_t buffer_size)
{
byte_t* p = *begin;
byte_t* buf_beg = buffer;
byte_t* buf_end = buffer + buffer_size;
enum float_state state = FP_START;
do {
switch(state) {
case FP_START:
if(*p == 'e' || *p == 'E')
state = FP_EXPONENT;
else if(*p == '.')
state = FP_DOT;
break;
case FP_EXPONENT:
if(*p == '+' || *p == '-')
state = FP_MINUSPLUS;
else if('0' > *p || *p > '9') {
failure("Unexpected character found in float literal after +/-");
} else
state = FP_FINAL;
break;
case FP_DOT:
state = FP_DECIMAL;
if('0' > *p || *p > '9')
failure("Unexpected character found in float literal after dot");
break;
case FP_DECIMAL:
if(*p == 'e' || *p == 'E')
state = FP_EXPONENT;
else if('0' > *p || *p > '9')
goto RETURN_TOKEN;
break;
case FP_MINUSPLUS:
state = FP_FINAL;
if('0' > *p || *p > '9')
failure("Unexpected character found in float literal");
break;
case FP_FINAL:
if('0' > *p || *p > '9')
goto RETURN_TOKEN;
break;
}
*buffer = *p;
if(buffer + 1 < buf_end) {
buffer++;
p++;
} else
failure("Bufferoverflow in number literal");
} while (*p != '\0');
RETURN_TOKEN:
*buffer = '\0';
*begin = p;
return TOK_FLOAT;
}
enum number_state {
INT_START,
INT_BASE,
......@@ -312,14 +230,20 @@ enum number_state {
INT_BIN_READ,
INT_HEX_READ,
INT_OCT_READ,
INT_DEC_READ
INT_DEC_READ,
FP_START,
FP_EXPONENT,
FP_DOT,
FP_DECIMAL,
FP_MINUSPLUS,
FP_FINAL
};
static enum token
lex_number(byte_t** begin, byte_t* buffer, size_t buffer_size)
lex_number(byte_t** src, byte_t* buffer, size_t buffer_size)
{
byte_t* p = *begin;
byte_t* p = *src;
byte_t* buf_beg = buffer;
byte_t* buf_end = buffer + buffer_size;
......@@ -342,9 +266,10 @@ lex_number(byte_t** begin, byte_t* buffer, size_t buffer_size)
state = INT_BIN_WAIT;
} else if('0' <= *p && *p <= '7') {
state = INT_OCT_READ;
break;
} else if(*p == '.' || *p == 'e' || *p == 'E') {
return lex_float(&p, buffer, buffer_size);
} else if(*p == '.') {
state = FP_DOT;
} else if(*p == 'e' || *p == 'E') {
state = FP_EXPONENT;
} else {
token = TOK_DEC;
goto RETURN_TOKEN;
......@@ -377,8 +302,10 @@ lex_number(byte_t** begin, byte_t* buffer, size_t buffer_size)
case INT_DEC_READ:
token = TOK_DEC;
if(*p == '.' || *p == 'e' || *p == 'E') {
return lex_float(&p, buffer, buffer_size);
if(*p == '.')
state = FP_DOT;
else if(*p == 'e' || *p == 'E') {
state = FP_EXPONENT;
} else if('0' > *p || *p > '9')
goto RETURN_TOKEN;
break;
......@@ -389,6 +316,40 @@ lex_number(byte_t** begin, byte_t* buffer, size_t buffer_size)
goto RETURN_TOKEN;
break;
case FP_EXPONENT:
if(*p == '+' || *p == '-')
state = FP_MINUSPLUS;
else if('0' > *p || *p > '9') {
failure("Unexpected character found in float literal after +/-");
} else
state = FP_FINAL;
break;
case FP_DOT:
state = FP_DECIMAL;
if('0' > *p || *p > '9')
failure("Unexpected character found in float literal after dot");
break;
case FP_DECIMAL:
if(*p == 'e' || *p == 'E')
state = FP_EXPONENT;
else if('0' > *p || *p > '9')
goto RETURN_TOKEN;
break;
case FP_MINUSPLUS:
state = FP_FINAL;
if('0' > *p || *p > '9')
failure("Unexpected character found in float literal");
break;
case FP_FINAL:
token = TOK_FLOAT;
if('0' > *p || *p > '9')
goto RETURN_TOKEN;
break;
default:
break;
......@@ -406,7 +367,7 @@ lex_number(byte_t** begin, byte_t* buffer, size_t buffer_size)
RETURN_TOKEN:
*buffer = '\0';
*begin = p;
*src = p;
return token;
}
......@@ -583,10 +544,6 @@ RETURN_TOKEN:
static enum token
lex(byte_t** begin, byte_t* buffer, size_t buffer_size)
{
#define SET_RETURN(token, p) \
begin = *(++p);
byte_t* p = *begin;
enum token tok = TOK_EOF;
......@@ -616,8 +573,9 @@ lex(byte_t** begin, byte_t* buffer, size_t buffer_size)
case '\'':
tok = TOK_QUOTE;
goto RETURN_AND_INC;
case ';':
while(*p != '\n' || *p != '\0')
while(*p != '\n' && *p != '\0')
p++;
continue;
......@@ -746,11 +704,12 @@ cherry_read(byte_t** src, byte_t* buffer, size_t buffer_size)
break;
case TOK_FLOAT:
val = floatpoint(strtod(buffer + 2, 0));
val = floatpoint(strtod(buffer, 0));
break;
case TOK_STRING:
val = string(string_dup(buffer));
break;
case TOK_SYMBOL:
val = symbol(string_dup(buffer));
......@@ -820,7 +779,7 @@ cherry_write_pair(FILE* out, struct value* value)
struct value* head = HEAD(value);
struct value* tail = TAIL(value);
write(out, head);
cherry_write(out, head);
if(IS_PAIR(tail)) {
fprintf(out, " ");
......@@ -968,9 +927,26 @@ cherry_write(FILE* out, struct value* value)
// main
// ----------------------------------------------------------------------------
#define SCANNER_BUFFERSIZE 2048
void cherry_main(const char* filename, const byte_t* method, struct value* args)
{
FILE* port = fopen(filename, "rb");
byte_t buffer[SCANNER_BUFFERSIZE];
if(!port)
failure("could not load file %s", filename);
byte_t* p = text(port);
struct value* exp = cherry_read(&p, buffer, SCANNER_BUFFERSIZE);
while(exp != 0) {
cherry_write(stdout, exp);
fprintf(stdout, "\n");
exp = cherry_read(&p, buffer, SCANNER_BUFFERSIZE);
}
exit(EXIT_SUCCESS);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment