Compare commits

..

2 Commits

Author SHA1 Message Date
bog fe0767bfe5 tuples in and index operations. 2024-03-19 08:56:41 +01:00
bog f9996d5910 bool arithmetic. 2024-03-19 07:11:28 +01:00
19 changed files with 624 additions and 18 deletions

View File

@ -25,7 +25,7 @@ int main(int argc, char** argv)
exec_module(&exec, &module);
if (!err_is_ok(&module.ccm.err)
if (!err_is_ok(&module.ccm.err)
|| !err_is_ok(&exec.err))
{
err_print_stack_trace(&module.ccm.err);
@ -45,6 +45,6 @@ int main(int argc, char** argv)
free_module:
module_free(&module);
}
return status;
}

View File

@ -1,16 +1,23 @@
MODULE ::= EXPR*
EXPR ::=
| TERM
| IN
| ASSERT
ASSERT ::= (assert_eq|assert_ne) tuple
IN ::= OR (in OR)?
OR ::= AND (or AND)*
AND ::= TERM (and TERM)*
TERM ::= FACTOR ((add|sub) FACTOR)*
FACTOR ::= USUB ((mul|div|mod) USUB)*
USUB ::= sub* POW
USUB ::= sub* NOT
NOT ::= not* POW
POW ::= LITERAL (pow LITERAL)?
LITERAL ::=
| BUILTIN
| TUPLE
| INDEX
| opar EXPR cpar
INDEX ::=
| TUPLE osquare (EXPR (comma EXPR)*)? csquare
TUPLE ::=
| opar EXPR+ cpar
BUILTIN ::= num
BUILTIN ::= num | bool

View File

@ -7,7 +7,8 @@
G(OP_PUSH), G(OP_POP), \
G(OP_ADD), G(OP_SUB), G(OP_USUB), G(OP_MUL), \
G(OP_DIV), G(OP_POW), G(OP_MOD), G(OP_MK_TUPLE), \
G(OP_ASSERT_EQ), G(OP_ASSERT_NE)
G(OP_ASSERT_EQ), G(OP_ASSERT_NE), G(OP_BRF), G(OP_BR), \
G(OP_NOT), G(OP_IN), G(OP_INDEX)
CCM_ENUM_H(Opcode, OPCODES);

107
lib/ccm.c
View File

@ -34,6 +34,51 @@ size_t ccm_str(ccm_t* self, char* buffer, size_t size)
return sz;
}
CCM ccm_from_value(ccm_t* self, value_t* value)
{
assert(self);
assert(value);
switch (value->type)
{
case TYPE_NUM: {
return ccm_to_num(self, value->data.num, value->line);
} break;
case TYPE_BOOLEAN: {
return ccm_to_boolean(
self,
value->data.boolean,
value->line
);
} break;
case TYPE_TUPLE: {
vec_t * vec = malloc(sizeof(vec_t));
vec_init(vec);
for (size_t i=0; i<value->data.tuple->size; i++)
{
vec_push(vec, value_new_clone(
value->data.tuple->data[i]
));
}
return ccm_to_tuple(
self,
vec,
value->line
);
} break;
default: {
fprintf(stderr,
"cannot convert value of type <%s> to CCM\n",
TypeStr[value->type]);
abort();
} break;
}
}
int ccm_is_num(ccm_t* self, CCM value)
{
value_t const* val = self->values.data[value];
@ -99,6 +144,27 @@ CCM ccm_to_tuple(ccm_t* self, vec_t* values, int line)
return self->values.size - 1;
}
int ccm_is_boolean(ccm_t* self, CCM value)
{
assert(self);
return ((value_t*) self->values.data[value])->type
== TYPE_BOOLEAN;
}
int ccm_from_boolean(ccm_t* self, CCM value)
{
assert(self);
return ((value_t*) self->values.data[value])->data.boolean;
}
CCM ccm_to_boolean(ccm_t* self, int value, int line)
{
value_t* boolean = malloc(sizeof(value_t));
value_init_boolean(boolean, value, line);
vec_push(&self->values, boolean);
return self->values.size - 1;
}
void ccm_push(ccm_t* self, CCM value)
{
assert(self);
@ -120,6 +186,36 @@ CCM ccm_top(ccm_t* self, int depth)
return (CCM) self->stack.data[self->stack.size - 1 - depth];
}
void ccm_in(ccm_t* self)
{
assert(self);
CCM ccm_rhs = ccm_pop(self);
CCM ccm_lhs = ccm_pop(self);
int line = ((value_t*) self->values.data[ccm_lhs])->line;
if (!ccm_is_tuple(self, ccm_rhs))
{
err_push(&self->err, line, "cannot test membership");
return;
}
vec_t* rhs = ccm_from_tuple(self, ccm_rhs);
value_t* val = ((value_t*)self->values.data[ccm_lhs]);
for (size_t i=0; i<rhs->size; i++)
{
if (value_equals(val, rhs->data[i]))
{
ccm_push(self, ccm_to_boolean(self, 1, line));
return;
}
}
ccm_push(self, ccm_to_boolean(self, 0, line));
}
void ccm_add(ccm_t* self)
{
assert(self);
@ -208,3 +304,14 @@ void ccm_pow(ccm_t* self)
ccm_push(self, ccm_to_num(self, powf(lhs, rhs), line));
}
void ccm_not(ccm_t* self)
{
assert(self);
CCM ccm_lhs = ccm_pop(self);
int line = ((value_t*) self->values.data[ccm_lhs])->line;
int lhs = ccm_from_boolean(self, ccm_lhs);
ccm_push(self, ccm_to_boolean(self, !lhs, line));
}

View File

@ -19,6 +19,8 @@ void ccm_free(ccm_t* self);
size_t ccm_str(ccm_t* self, char* buffer, size_t size);
CCM ccm_from_value(ccm_t* self, value_t* value);
int ccm_is_num(ccm_t* self, CCM value);
double ccm_from_num(ccm_t* self, CCM value);
CCM ccm_to_num(ccm_t* self, double value, int line);
@ -27,10 +29,15 @@ int ccm_is_tuple(ccm_t* self, CCM value);
vec_t* ccm_from_tuple(ccm_t* self, CCM value);
CCM ccm_to_tuple(ccm_t* self, vec_t* values, int line);
int ccm_is_boolean(ccm_t* self, CCM value);
int ccm_from_boolean(ccm_t* self, CCM value);
CCM ccm_to_boolean(ccm_t* self, int value, int line);
void ccm_push(ccm_t* self, CCM value);
CCM ccm_pop(ccm_t* self);
CCM ccm_top(ccm_t* self, int depth);
void ccm_in(ccm_t* self);
void ccm_add(ccm_t* self);
void ccm_sub(ccm_t* self);
void ccm_usub(ccm_t* self);
@ -38,5 +45,6 @@ void ccm_mul(ccm_t* self);
void ccm_div(ccm_t* self);
void ccm_mod(ccm_t* self);
void ccm_pow(ccm_t* self);
void ccm_not(ccm_t* self);
#endif

View File

@ -13,6 +13,9 @@
#define CCM_ENUM_ENUM(X) X
#define CCM_ENUM_STR(X) #X
#define CCM_DEBUG(TARGET, FUN) \
{char m[CCM_STRLEN]; FUN(TARGET, m, CCM_STRLEN), printf("%s\n", m);}
#define CCM_ENUM_H(PREFIX, DEF) \
typedef enum { DEF(CCM_ENUM_ENUM) } PREFIX ; \
extern char const* PREFIX ## Str []

View File

@ -26,13 +26,43 @@ void compiler_compile(compiler_t* self,
return;
}
ccm_t* ccm = &self->module->ccm;
switch (node->kind)
{
case NODE_INDEX: {
for (size_t i=0; i<node->children.size; i++)
{
size_t k = node->children.size - 1 - i;
compiler_compile(self, node->children.data[k], prog);
}
prog_add_instr(prog, OP_INDEX, node->children.size - 1);
} break;
case NODE_IN: {
compiler_compile(self, node->children.data[0], prog);
compiler_compile(self, node->children.data[1], prog);
prog_add_instr(prog, OP_IN, CCM_NO_PARAM);
} break;
case NODE_AND: {
compiler_compile_and(self, node, prog);
} break;
case NODE_OR: {
compiler_compile_or(self, node, prog);
} break;
case NODE_NOT: {
compiler_compile(self, node->children.data[0], prog);
prog_add_instr(prog, OP_NOT, CCM_NO_PARAM);
} break;
case NODE_ASSERT_EQ: {
compiler_compile(self, node->children.data[0], prog);
prog_add_instr(prog, OP_ASSERT_EQ, CCM_NO_PARAM);
} break;
case NODE_ASSERT_NE: {
compiler_compile(self, node->children.data[0], prog);
prog_add_instr(prog, OP_ASSERT_NE, CCM_NO_PARAM);
@ -71,6 +101,13 @@ void compiler_compile(compiler_t* self,
prog_add_instr(prog, OP_MK_TUPLE, node->children.size);
} break;
case NODE_BOOL: {
int val = strcmp(node->value, "true") == 0;
CCM value = ccm_to_boolean(ccm, val, node->line);
size_t id = prog_add_new_constant(prog, value);
prog_add_instr(prog, OP_PUSH, id);
} break;
case NODE_SUB: {
compiler_compile(self, node->children.data[0], prog);
@ -114,3 +151,91 @@ void compiler_compile(compiler_t* self,
}
}
}
void compiler_compile_or(compiler_t* self,
node_t* node,
prog_t* prog)
{
ccm_t* ccm = &self->module->ccm;
vec_t to_true;
vec_init(&to_true);
for (size_t i=0; i<node->children.size; i++)
{
compiler_compile(self, node->children.data[i], prog);
prog_add_instr(prog, OP_NOT, 0);
size_t addr = prog_add_instr(prog, OP_BRF, 0);
vec_push(&to_true, (void*) addr);
}
// FALSE
prog_add_instr(prog, OP_PUSH, prog_add_new_constant(
prog,
ccm_to_boolean(ccm, 0, node->line)
));
size_t to_end = prog_add_instr(prog, OP_BR, 0);
// TRUE
size_t true_point = prog->instrs.size;
prog_add_instr(prog, OP_PUSH, prog_add_new_constant(
prog,
ccm_to_boolean(ccm, 1, node->line)
));
for (size_t i=0; i<to_true.size; i++)
{
size_t k = ((size_t) to_true.data[i]);
((instr_t*) prog->instrs.data[k])->param = true_point;
}
size_t end_point = prog->instrs.size;
((instr_t*) prog->instrs.data[to_end])->param = end_point;
vec_free(&to_true);
}
void compiler_compile_and(compiler_t* self,
node_t* node,
prog_t* prog)
{
ccm_t* ccm = &self->module->ccm;
vec_t to_false;
vec_init(&to_false);
for (size_t i=0; i<node->children.size; i++)
{
compiler_compile(self, node->children.data[i], prog);
size_t addr = prog_add_instr(prog, OP_BRF, 0);
vec_push(&to_false, (void*) addr);
}
// TRUE
prog_add_instr(prog, OP_PUSH, prog_add_new_constant(
prog,
ccm_to_boolean(ccm, 1, node->line)
));
size_t to_end = prog_add_instr(prog, OP_BR, 0);
// FALSE
size_t false_point = prog_add_instr(
prog,
OP_PUSH,
prog_add_new_constant(
prog,
ccm_to_boolean(ccm, 0, node->line)
)
);
for (size_t i=0; i<to_false.size; i++)
{
size_t k = ((size_t) to_false.data[i]);
((instr_t*) prog->instrs.data[k])->param = false_point;
}
size_t end_point = prog->instrs.size;
((instr_t*) prog->instrs.data[to_end])->param = end_point;
vec_free(&to_false);
}

View File

@ -16,7 +16,15 @@ typedef struct {
void compiler_init(compiler_t* self, module_t* module);
void compiler_free(compiler_t* self);
void compiler_compile(compiler_t* self,
node_t* node,
void compiler_compile(compiler_t* self,
node_t* node,
prog_t* prog);
void compiler_compile_or(compiler_t* self,
node_t* node,
prog_t* prog);
void compiler_compile_and(compiler_t* self,
node_t* node,
prog_t* prog);
#endif

View File

@ -44,6 +44,73 @@ void exec_instr(exec_t* self,
switch (op)
{
case OP_INDEX: {
CCM ccm_target = ccm_pop(ccm);
vec_t* target = ccm_from_tuple(ccm, ccm_target);
value_t* result = NULL;
for (int i=0; i<param; i++)
{
CCM ccm_idx = ccm_pop(ccm);
int idx = ccm_from_num(ccm, ccm_idx);
if (idx < 0)
{
idx = target->size + idx;
}
if (idx < 0 || idx >= (ssize_t) target->size)
{
assert(target->size > 0);
err_push(&self->err, ((value_t*)target->data[0])->line,
"index out of bounds");
return;
}
value_t* val = target->data[idx];
if (i == param - 1)
{
result = val;
}
else
{
target = val->data.tuple;
}
}
ccm_push(ccm, ccm_from_value(ccm, result));
self->pc++;
} break;
case OP_NOT: {
ccm_not(ccm);
self->pc++;
} break;
case OP_IN: {
ccm_in(ccm);
self->pc++;
} break;
case OP_BR: {
self->pc = param;
} break;
case OP_BRF: {
CCM value = ccm_pop(ccm);
int val = ccm_from_boolean(ccm, value);
if (val)
{
self->pc++;
}
else
{
self->pc = param;
}
} break;
case OP_ASSERT_NE:
case OP_ASSERT_EQ: {
CCM val = ccm_pop(ccm);
@ -64,7 +131,7 @@ void exec_instr(exec_t* self,
char rhs[CCM_STRLEN];
value_str(values->data[1], rhs, CCM_STRLEN);
char const* operator = oracle ? "==" : "!=";
char const* operator = oracle ? "==" : "!=";
err_push(
&self->err,

View File

@ -24,6 +24,8 @@ void lexer_init(lexer_t* self)
lexer_add_text(self, "/", NODE_DIV);
lexer_add_text(self, "^", NODE_POW);
lexer_add_text(self, "%", NODE_MOD);
lexer_add_text(self, "[", NODE_OSQUARE);
lexer_add_text(self, "]", NODE_CSQUARE);
}
void lexer_free(lexer_t* self)
@ -159,6 +161,12 @@ node_t* lexer_try_new_next(lexer_t* self)
CCM_KEYWORD("assert_eq", NODE_ASSERT_EQ, 0);
CCM_KEYWORD("assert_ne", NODE_ASSERT_NE, 0);
CCM_KEYWORD("true", NODE_BOOL, 1);
CCM_KEYWORD("false", NODE_BOOL, 1);
CCM_KEYWORD("and", NODE_AND, 0);
CCM_KEYWORD("or", NODE_OR, 0);
CCM_KEYWORD("not", NODE_NOT, 0);
CCM_KEYWORD("in", NODE_IN, 0);
if (self->cursor < (ssize_t) strlen(self->source))
{

View File

@ -46,6 +46,11 @@ int module_load(module_t* self, char const* path)
node_t* ast = parser_try_new_parse(&parser);
if (!ast)
{
goto free_parser;
}
if (!err_is_ok(&lexer.err) || !err_is_ok(&parser.err))
{
err_print_stack_trace(&lexer.err);
@ -53,7 +58,6 @@ int module_load(module_t* self, char const* path)
err_push(&self->err, lexer.line, "invalid module");
goto free_parser;
}
compiler_t compiler;
compiler_init(&compiler, self);

View File

@ -9,7 +9,9 @@ G(NODE_MODULE), \
G(NODE_NUM), G(NODE_OPAR), G(NODE_CPAR), \
G(NODE_POW), G(NODE_ADD), G(NODE_SUB), G(NODE_MUL), \
G(NODE_DIV), G(NODE_MOD), G(NODE_COMMA), G(NODE_TUPLE), \
G(NODE_ASSERT_EQ), G(NODE_ASSERT_NE)
G(NODE_ASSERT_EQ), G(NODE_ASSERT_NE), G(NODE_BOOL), \
G(NODE_AND), G(NODE_OR), G(NODE_NOT), G(NODE_IN), \
G(NODE_OSQUARE), G(NODE_CSQUARE), G(NODE_INDEX)
CCM_ENUM_H(NodeKind, NODE_KIND);

View File

@ -1,6 +1,8 @@
#include "parser.h"
#define CCM_TRY(rule) parser_try_new_rule(self, rule)
#define CCM_TRY_LL1(rule, node) \
parser_try_new_rule_ll1(self, rule, node)
void parser_init(parser_t* self, lexer_t* lexer)
{
@ -23,6 +25,25 @@ node_t* parser_try_new_parse(parser_t* self)
return CCM_TRY(parser_try_new_module);
}
node_t* parser_try_new_rule_ll1(parser_t* self,
rule_ll1_t rule,
node_t* node)
{
if (!err_is_ok(&self->err))
{
return NULL;
}
lexer_state_t state = lexer_state(self->lexer);
node_t* result = rule(self, node);
if (result) { return result; }
lexer_restore(self->lexer, state);
return (void*) NULL;
}
node_t* parser_try_new_rule(parser_t* self, rule_t rule)
{
if (!err_is_ok(&self->err))
@ -92,14 +113,14 @@ node_t* parser_try_new_module(parser_t* self)
node_t* parser_try_new_expr(parser_t* self)
{
assert(self);
if (lexer_peek_kind(self->lexer, NODE_ASSERT_EQ, 0)
|| lexer_peek_kind(self->lexer, NODE_ASSERT_NE, 0))
{
return CCM_TRY(parser_try_new_assert);
}
return CCM_TRY(parser_try_new_term);
return CCM_TRY(parser_try_new_in);
}
node_t* parser_try_new_assert(parser_t* self)
@ -122,7 +143,7 @@ node_t* parser_try_new_assert(parser_t* self)
free(node);
return NULL;
}
node_t* tuple = CCM_TRY(parser_try_new_tuple);
@ -138,6 +159,96 @@ node_t* parser_try_new_assert(parser_t* self)
return node;
}
node_t* parser_try_new_in(parser_t* self)
{
assert(self);
node_t* lhs = CCM_TRY(parser_try_new_or);
if (!lhs) { return NULL; }
if (lexer_peek_kind(self->lexer, NODE_IN, 0))
{
lexer_consume_next(self->lexer, NODE_IN);
node_t* node = malloc(sizeof(node_t));
node_init(node, NODE_IN, "", lhs->line);
node_t* rhs = CCM_TRY(parser_try_new_or);
if (!rhs)
{
node_free(lhs); free(lhs);
node_free(node); free(node);
return NULL;
}
node_push_new_child(node, lhs);
node_push_new_child(node, rhs);
lhs = node;
}
return lhs;
}
node_t* parser_try_new_or(parser_t* self)
{
assert(self);
node_t* lhs = CCM_TRY(parser_try_new_and);
if (!lhs) { return NULL; }
while (lexer_peek_kind(self->lexer, NODE_OR, 0))
{
lexer_consume_next(self->lexer, NODE_OR);
node_t* node = malloc(sizeof(node_t));
node_init(node, NODE_OR, "", lhs->line);
node_push_new_child(node, lhs);
node_t* rhs = CCM_TRY(parser_try_new_and);
if (!rhs)
{
node_free(lhs);
free(lhs);
node_free(node);
free(node);
return NULL;
}
node_push_new_child(node, rhs);
lhs = node;
}
return lhs;
}
node_t* parser_try_new_and(parser_t* self)
{
assert(self);
node_t* lhs = CCM_TRY(parser_try_new_term);
if (!lhs) { return NULL; }
while (lexer_peek_kind(self->lexer, NODE_AND, 0))
{
lexer_consume_next(self->lexer, NODE_AND);
node_t* node = malloc(sizeof(node_t));
node_init(node, NODE_AND, "", lhs->line);
node_push_new_child(node, lhs);
node_t* rhs = CCM_TRY(parser_try_new_term);
if (!rhs)
{
node_free(lhs);
free(lhs);
node_free(node);
free(node);
return NULL;
}
node_push_new_child(node, rhs);
lhs = node;
}
return lhs;
}
node_t* parser_try_new_term(parser_t* self)
{
assert(self);
@ -223,6 +334,38 @@ node_t* parser_try_new_usub(parser_t* self)
return node;
}
else
{
return CCM_TRY(parser_try_new_not);
}
return NULL;
}
node_t* parser_try_new_not(parser_t* self)
{
assert(self);
if (lexer_peek_kind(self->lexer, NODE_NOT, 0))
{
lexer_consume_next(self->lexer, NODE_NOT);
node_t* node = malloc(sizeof(node_t));
node_init(node, NODE_NOT, "", self->lexer->line);
node_t* rhs = CCM_TRY(parser_try_new_not);
if (!rhs)
{
node_free(node);
free(node);
return NULL;
}
node_push_new_child(node, rhs);
return node;
}
else
{
return CCM_TRY(parser_try_new_pow);
}
@ -276,6 +419,11 @@ node_t* parser_try_new_literal(parser_t* self)
if (tuple)
{
if (lexer_peek_kind(self->lexer, NODE_OSQUARE, 0))
{
return CCM_TRY_LL1(parser_try_new_index, tuple);
}
return tuple;
}
@ -303,6 +451,44 @@ node_t* parser_try_new_literal(parser_t* self)
return CCM_TRY(parser_try_new_builtin);
}
node_t* parser_try_new_index(parser_t* self, node_t* target)
{
assert(self);
lexer_consume_next(self->lexer, NODE_OSQUARE);
node_t* node = malloc(sizeof(node_t));
node_init(node, NODE_INDEX, "", target->line);
node_push_new_child(node, target);
while (!lexer_peek_kind(self->lexer, NODE_CSQUARE, 0))
{
node_t* element = CCM_TRY(parser_try_new_expr);
if (!element)
{
node_free(node); free(node);
return NULL;
}
node_push_new_child(node, element);
if (!lexer_peek_kind(self->lexer, NODE_CSQUARE, 0))
{
lexer_consume_next(self->lexer, NODE_COMMA);
}
}
lexer_consume_next(self->lexer, NODE_CSQUARE);
return node;
}
node_t* parser_try_new_expr_lst(parser_t* self)
{
assert(self);
}
node_t* parser_try_new_tuple(parser_t* self)
{
assert(self);
@ -363,9 +549,13 @@ node_t* parser_try_new_builtin(parser_t* self)
assert(self);
node_t* node = lexer_try_new_next(self->lexer);
parser_ensure(self, node, NODE_NUM);
if (node && node->kind == NODE_NUM)
if (node &&
(
node->kind == NODE_NUM
|| node->kind == NODE_BOOL
)
)
{
return node;
}

View File

@ -13,22 +13,30 @@ typedef struct {
} parser_t;
typedef node_t* (*rule_t)(parser_t*);
typedef node_t* (*rule_ll1_t)(parser_t*, node_t*);
void parser_init(parser_t* self, lexer_t* lexer);
void parser_free(parser_t* self);
node_t* parser_try_new_parse(parser_t* self);
node_t* parser_try_new_rule(parser_t* self, rule_t rule);
node_t* parser_try_new_rule_ll1(parser_t* self, rule_ll1_t rule, node_t* node);
int parser_ensure(parser_t* self, node_t* node, NodeKind kind);
node_t* parser_try_new_module(parser_t* self);
node_t* parser_try_new_expr(parser_t* self);
node_t* parser_try_new_assert(parser_t* self);
node_t* parser_try_new_in(parser_t* self);
node_t* parser_try_new_or(parser_t* self);
node_t* parser_try_new_and(parser_t* self);
node_t* parser_try_new_term(parser_t* self);
node_t* parser_try_new_factor(parser_t* self);
node_t* parser_try_new_usub(parser_t* self);
node_t* parser_try_new_not(parser_t* self);
node_t* parser_try_new_pow(parser_t* self);
node_t* parser_try_new_literal(parser_t* self);
node_t* parser_try_new_index(parser_t* self, node_t* target);
node_t* parser_try_new_expr_lst(parser_t* self);
node_t* parser_try_new_tuple(parser_t* self);
node_t* parser_try_new_builtin(parser_t* self);

View File

@ -5,7 +5,8 @@
#define TYPES(G) \
G(TYPE_NUM), \
G(TYPE_TUPLE)
G(TYPE_TUPLE), \
G(TYPE_BOOLEAN)
CCM_ENUM_H(Type, TYPES);

View File

@ -17,6 +17,14 @@ void value_init_new_tuple(value_t* self, vec_t* values, int line)
self->line = line;
}
void value_init_boolean(value_t* self, int boolean, int line)
{
assert(self);
self->data.boolean = boolean;
self->type = TYPE_BOOLEAN;
self->line = line;
}
value_t* value_new_clone(value_t* self)
{
assert(self);
@ -28,6 +36,9 @@ value_t* value_new_clone(value_t* self)
case TYPE_NUM: {
value_init_num(value, self->data.num, self->line);
} break;
case TYPE_BOOLEAN: {
value_init_boolean(value, self->data.boolean, self->line);
} break;
case TYPE_TUPLE: {
vec_t* vec = malloc(sizeof(vec_t));
vec_init(vec);
@ -76,6 +87,11 @@ size_t value_str(value_t* self, char* buffer, size_t size)
self->data.num);
} break;
case TYPE_BOOLEAN: {
sz += snprintf(buffer + sz, size - sz, "%s",
self->data.boolean ? "true" : "false");
} break;
case TYPE_TUPLE: {
sz += snprintf(buffer + sz, size - sz, "(");
@ -117,6 +133,10 @@ int value_equals(value_t* self, value_t* rhs)
return self->data.num == rhs->data.num;
} break;
case TYPE_BOOLEAN: {
return self->data.boolean == rhs->data.boolean;
} break;
case TYPE_TUPLE: {
if (self->data.tuple->size != rhs->data.tuple->size)
{

View File

@ -9,6 +9,7 @@ typedef struct {
union {
double num;
vec_t* tuple;
int boolean;
} data;
Type type;
@ -17,6 +18,7 @@ typedef struct {
void value_init_num(value_t* self, double num, int line);
void value_init_new_tuple(value_t* self, vec_t* values, int line);
void value_init_boolean(value_t* self, int boolean, int line);
value_t* value_new_clone(value_t* self);
void value_free(value_t* self);

20
tests/bool.ccm Normal file
View File

@ -0,0 +1,20 @@
assert_eq (true, true)
assert_eq (false, false)
assert_eq (true, true and true)
assert_eq (false, true and false)
assert_eq (false, false and true)
assert_eq (false, false and false)
assert_eq (true, true or true)
assert_eq (true, true or false)
assert_eq (true, false or true)
assert_eq (false, false or false)
assert_eq (true, not false)
assert_eq (false, not true)
assert_eq (true, not not true)
assert_eq (false, not not not true)

25
tests/tuple.ccm Normal file
View File

@ -0,0 +1,25 @@
assert_eq ((3, 2), (3, 2))
assert_ne ((3, 2), (3, 4))
# IN KEYWORD
# ==========
assert_eq (true, 7 in (2, 7, 6))
assert_eq (false, 7 in (2, 2, 6))
assert_eq (true, (2, 3) in (1, (2, 3)))
assert_eq (false, (2, 3) in (1, (2, 4)))
# INDEX
# =====
assert_eq (2, (2, 7)[0])
assert_eq (7, (2, 7)[1])
assert_eq (9, (2, 7, 9)[-1])
assert_eq (7, (2, 7, 9)[-2])
assert_eq (2, (2, 7, 9)[-3])
assert_eq (1, ((1, 2), (3, 4))[0, 0])
assert_eq (2, ((1, 2), (3, 4))[0, 1])
assert_eq (3, ((1, 2), (3, 4))[1, 0])
assert_eq (4, ((1, 2), (3, 4))[1, 1])
assert_eq (false, 23 in (2, (7, 23, 2), 9))
assert_eq (true, 23 in (2, (7, 23, 2), 9)[1])