From c899a530f99d44db7c30c0eb0b4ba0d5f6a2cb8c Mon Sep 17 00:00:00 2001 From: nishi Date: Sun, 5 May 2024 16:45:33 +0000 Subject: [PATCH] condition works git-svn-id: file:///raid/svn-main/nishi-serenade/trunk@110 0f02c867-ac3d-714e-8a88-971ba1f6efcf --- Serenade/interpreter.c | 54 ++++++++++++++++++++++++++++++++++++++++++ Serenade/parser.c | 2 +- 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/Serenade/interpreter.c b/Serenade/interpreter.c index 60faa41..4cc8375 100644 --- a/Serenade/interpreter.c +++ b/Serenade/interpreter.c @@ -94,6 +94,57 @@ struct sn_generic* cond_handler(struct sn_interpreter* sn, int args, struct sn_g return gen; } +struct sn_generic* cond1_handler(struct sn_interpreter* sn, int args, struct sn_generic** gens) { + struct sn_generic* gen = malloc(sizeof(struct sn_generic)); + gen->type = SN_TYPE_DOUBLE; + gen->number = 0; + int i; + if(args != 2 || !(gens[1]->type == SN_TYPE_DOUBLE)) { + gen->number = -1; + } else { + double a = gens[1]->number; + if(strcmp(gens[0]->name, "not") == 0) { + gen->number = a == 0 ? 1 : 0; + } + } + return gen; +} + +struct sn_generic* condn_handler(struct sn_interpreter* sn, int args, struct sn_generic** gens) { + struct sn_generic* gen = malloc(sizeof(struct sn_generic)); + gen->type = SN_TYPE_DOUBLE; + gen->number = 0; + int i; + if(args < 2) { + gen->number = -1; + } else { + int i; + for(i = 1; i < args; i++){ + if(gens[i]->type != SN_TYPE_DOUBLE){ + gen->number = -1; + return gen; + } + } + if(strcmp(gens[0]->name, "or") == 0) { + for(i = 1; i < args; i++){ + if(gens[i]->number != 0){ + gen->number = 1; + break; + } + } + }else if(strcmp(gens[0]->name, "and") == 0) { + gen->number = 1; + for(i = 1; i < args; i++){ + if(gens[i]->number == 0){ + gen->number = 0; + break; + } + } + } + } + return gen; +} + struct sn_generic* print_handler(struct sn_interpreter* sn, int args, struct sn_generic** gens) { struct sn_generic* gen = malloc(sizeof(struct sn_generic)); int i; @@ -178,6 +229,9 @@ void sn_stdlib_init(struct sn_interpreter* sn) { sn_set_handler(sn, ">=", cond_handler); sn_set_handler(sn, "<", cond_handler); sn_set_handler(sn, ">", cond_handler); + sn_set_handler(sn, "not", cond1_handler); + sn_set_handler(sn, "or", condn_handler); + sn_set_handler(sn, "and", condn_handler); sn_set_handler(sn, "print", print_handler); sn_set_handler(sn, "eval", eval_handler); sn_set_handler(sn, "define-variable", defvar_handler); diff --git a/Serenade/parser.c b/Serenade/parser.c index d23a7df..930cf39 100644 --- a/Serenade/parser.c +++ b/Serenade/parser.c @@ -162,7 +162,7 @@ struct sn_generic* sn_expr_parse(char* data, unsigned long long size) { free(tmp); if(argbufmode == SN_TYPE_VOID) { - if(c == '.' || (c >= '0' && c <= '9')) { + if(c == '.' || c == '-' || (c >= '0' && c <= '9')) { argbufmode = SN_TYPE_DOUBLE; } else if(index_stack[br - 1] == 0) { argbufmode = SN_TYPE_FUNCTION; -- 2.43.0