Trying a more object oriented approach and created an 'Object' struct to have metadata for the values the interpreter works with.
This commit is contained in:
+20
-11
@@ -1,9 +1,9 @@
|
||||
#include "interpreter.h"
|
||||
#include "token.h"
|
||||
|
||||
void* Evaluate(Expr*);
|
||||
int IsTruthy(void*);
|
||||
int IsEqual(void*, void*);
|
||||
Object* Evaluate(Expr*);
|
||||
int IsTruthy(Object*);
|
||||
int IsEqual(Object*, Object*);
|
||||
|
||||
const void* VisitLiteralExpression(Expr* expression) {
|
||||
return expression->expression.Literal->literal;
|
||||
@@ -66,22 +66,31 @@ void* VisitUnaryExpression(Expr* expression) {
|
||||
return NULL; //Should be unreachable.
|
||||
}
|
||||
|
||||
void* Evaluate(Expr* expression) {
|
||||
Object* Evaluate(Expr* expression) {
|
||||
//accept
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int IsTruthy(void* object) {
|
||||
if (!object) return 0;
|
||||
if (*((int *) object) == 0) return 0;
|
||||
int IsTruthy(Object* object) {
|
||||
if (!object || object->instance == NIL) return 0;
|
||||
if (object->instance == TRUE) return 0;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int IsEqual(void* a, void* b) {
|
||||
if (!a && !b) return 1;
|
||||
if (!a) return 0;
|
||||
int IsEqual(Object* a, Object* b) {
|
||||
if (a->instance == NIL && b->instance == NIL) return 1;
|
||||
if (a->instance == NIL) return 0;
|
||||
|
||||
if (b->instance == Number && b->instance == Number) {
|
||||
return a->value.number == b->value.number;
|
||||
}
|
||||
|
||||
if (a->instance == String && b->instance == String) {
|
||||
int result = strcmp(a->value.string, b->value.string);
|
||||
|
||||
return result == 0;
|
||||
}
|
||||
|
||||
//Oh this'll be fun...
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user