diff --git a/example/hello.wren b/example/hello.wren index 0cfbf088..43983eb2 100644 --- a/example/hello.wren +++ b/example/hello.wren @@ -1 +1 @@ -2 +21212 diff --git a/src/compiler.c b/src/compiler.c index c47b3140..3e51236b 100644 --- a/src/compiler.c +++ b/src/compiler.c @@ -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; diff --git a/src/main.c b/src/main.c index 63d8cc3d..556e3f12 100644 --- a/src/main.c +++ b/src/main.c @@ -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;