1
0
forked from Mirror/wren

Actually parse number literal.

This commit is contained in:
Bob Nystrom
2013-10-22 13:37:53 -07:00
parent 0b1c42a818
commit b1867f054a
3 changed files with 14 additions and 3 deletions

View File

@ -1 +1 @@
2
21212

View File

@ -341,13 +341,23 @@ void compilePrecedence(Compiler* compiler, int precedence)
void prefixLiteral(Compiler* compiler, Token* token)
{
// TODO(bob): Get actual value from token!
char* end;
long value = strtol(compiler->source + token->start, &end, 10);
// TODO(bob): Check errno == ERANGE here.
if (end == compiler->source + token->start)
{
error(compiler, "Invalid number literal.");
value = 0;
}
// Define a constant for the literal.
// TODO(bob): See if constant with same value already exists.
Value constant = malloc(sizeof(Obj));
constant->type = OBJ_INT;
constant->flags = 0;
constant->value = 234;
// TODO(bob): Handle truncation!
constant->value = (int)value;
compiler->block->constants[compiler->block->numConstants++] = constant;

View File

@ -29,6 +29,7 @@ int main(int argc, const char * argv[])
Fiber* fiber = newFiber();
Value value = interpret(fiber, block);
printValue(value);
printf("\n");
free(source);
return 0;