diff options
| author | Kai Stevenson <kai@kaistevenson.com> | 2023-02-15 15:53:33 -0800 |
|---|---|---|
| committer | Kai Stevenson <kai@kaistevenson.com> | 2023-02-15 15:53:33 -0800 |
| commit | f5985c65df468e49109febcad2a3937aea801b89 (patch) | |
| tree | 6c3d9bd1074c972f013f628d72a91374b8d98f5a /precedent.c | |
| parent | 7e696a40e51aa35b003aa9c3fc72139b34cddc71 (diff) | |
types. eq() internal function
Diffstat (limited to 'precedent.c')
| -rw-r--r-- | precedent.c | 67 |
1 files changed, 56 insertions, 11 deletions
diff --git a/precedent.c b/precedent.c index 95d62e5..9d60bfd 100644 --- a/precedent.c +++ b/precedent.c @@ -1,13 +1,18 @@ #include <stdlib.h> #include <stdio.h> +#include <string.h> #include "precedent.h" #include "hashtable.h" #include "parse.h" hashtable_t* ht; +const int trueVal = 1; +sym_t trueSym = {LITERAL, INT, (void*)&trueVal, NULL, 0}; +const int falseVal = 0; +sym_t falseSym = {LITERAL, INT, (void*)&falseVal, NULL, 0}; int main() { - sym_t* finalSymbol = parse_symbol_final("store(print(hello;) myFunc;) myFunc()"); + sym_t* finalSymbol = parse_symbol_final("print(eq(0; 1;))"); printf("drawing program:\n"); write_symbol_visual(finalSymbol, 0); printf("\nexecuting program:\n"); @@ -23,12 +28,12 @@ void exec_all(symset_t symset) { eval(symset.symbols[i]); } } -void* eval(sym_t* symbol) { +sym_t* eval(sym_t* symbol) { if (symbol->province == LITERAL) { - return symbol->name; + return(symbol); } else if (symbol->province == KEY_REF) { - void* ir = try_eval_internal(symbol); + sym_t* ir = try_eval_internal(symbol); if (ir != NULL) { return ir; } @@ -51,30 +56,70 @@ void* eval(sym_t* symbol) { } /* internal functions? this function has side-effects */ -void* try_eval_internal(sym_t* symbol) { +sym_t* try_eval_internal(sym_t* symbol) { unsigned int hash = get_hash((char*)symbol->name); + /* TODO: return last arg. that's the function return line */ if (hash == get_hash("root")) { /* execute all args as roots */ symset_t symset; symset.symbols = symbol->args; symset.count = symbol->argCount; exec_all(symset); - return (void*)1; + return &trueSym; } else if (hash == get_hash("store")) { /* TODO: scope */ /* first arg is value, remaining args are names */ /* TODO: recursion for multiple pairs */ - char* key = (char*)eval(symbol->args[1]); + sym_t* v = eval(symbol->args[0]); + char* key = symtos(v); sym_t* value = symbol->args[0]; pair_t* p = init_pair(key, (void*)value); hashtable_add_pair(ht, p); - return (void*)1; + return &trueSym; + } + else if (hash == get_hash("eq")) { + litType_t type = NO_TYPE; + void* lastVal = NULL; + for (int i = 0; i < symbol->argCount; i++) { + sym_t* v = eval(symbol->args[i]); + /* initialize */ + if (type == NO_TYPE) { + type = v->litType; + lastVal = v->name; + } + else { + /* interpret type */ + if (type == INT) { + if (symtoi(v) != *((int*)lastVal)) { + return &falseSym; + } + } + if (type == STRING) { + if (strcmp(symtos(v), (char*)lastVal) != 0) { + return &falseSym; + } + } + /* TODO: float */ + } + } + return &trueSym; } else if (hash == get_hash("print")) { - /* TODO: genericize printf type */ - printf("%s\n", (char*)eval(symbol->args[0])); - return (void*)1; + sym_t* v = eval(symbol->args[0]); + switch (v->litType) { + case NO_TYPE: + break; + case INT: + printf("%d\n", symtoi(v)); + break; + case FLOAT: + break; + case STRING: + printf("%s\n", symtos(v)); + break; + } + return &trueSym; } else { return NULL; |
