100 lines
1.8 KiB
C
100 lines
1.8 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
#include "primitives.h"
|
|
|
|
datum* add(datum* args) {
|
|
int32_t result = 0;
|
|
while (args) {
|
|
if (args->type != datum_type_i32) {
|
|
printf("ADD - invalid type\n");
|
|
exit(-1);
|
|
}
|
|
result += args->value;
|
|
args = args->next;
|
|
}
|
|
return const_i32(result);
|
|
}
|
|
|
|
datum* sub(datum* args) {
|
|
int32_t result;
|
|
|
|
if (args) {
|
|
result = args->value;
|
|
} else {
|
|
exit(-1);
|
|
}
|
|
|
|
if (!args->next) {
|
|
result = -result;
|
|
} else {
|
|
args = args->next;
|
|
while (args) {
|
|
if (args->type != datum_type_i32) {
|
|
printf("SUB - invalid type\n");
|
|
exit(-1);
|
|
}
|
|
result -= args->value;
|
|
args = args->next;
|
|
}
|
|
}
|
|
|
|
return const_i32(result);
|
|
}
|
|
|
|
datum* mul(datum* args) {
|
|
int32_t result = 1;
|
|
|
|
while (args) {
|
|
if (args->type != datum_type_i32) {
|
|
printf("MUL - invalid type\n");
|
|
exit(-1);
|
|
}
|
|
result *= args->value;
|
|
args = args->next;
|
|
}
|
|
|
|
return const_i32(result);
|
|
}
|
|
|
|
datum* eq(datum* args) {
|
|
if (!args->next) {
|
|
return const_bool(1);
|
|
}
|
|
|
|
datum *first = args;
|
|
while (first->next) {
|
|
if (!datum_eq(first, first->next))
|
|
return const_bool(0);
|
|
first = first->next;
|
|
}
|
|
|
|
return const_bool(1);
|
|
}
|
|
|
|
datum* lt(datum* args) {
|
|
if (!args->next) {
|
|
return const_bool(1);
|
|
}
|
|
|
|
datum *first = args;
|
|
while (first->next) {
|
|
if (!datum_lt(first, first->next))
|
|
return const_bool(0);
|
|
first = first->next;
|
|
}
|
|
|
|
return const_bool(1);
|
|
}
|
|
|
|
datum* display(datum* args) {
|
|
print_datum(args);
|
|
return NULL;
|
|
}
|
|
|
|
datum* newline(datum* args) {
|
|
printf("\n");
|
|
return NULL;
|
|
}
|
|
|