Files
sc/assets/prelude.h

95 lines
1.4 KiB
C

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
struct Int;
struct String;
union Value;
enum Tag { INT, FLOAT, STRING };
typedef union Value (*Lambda)() ;
struct Int {
enum Tag t;
int value;
};
struct Float {
enum Tag t;
float value;
};
struct String {
enum Tag t;
char* value;
};
union Value {
enum Tag t;
struct Int i;
struct Float f;
struct String s;
} ;
typedef union Value Value;
static Value MakeInt(int n) {
Value v;
v.i.t = INT;
v.i.value = n;
return v;
}
static Value MakeFloat(float n) {
Value v;
v.f.t = FLOAT;
v.f.value = n;
return v;
}
static Value MakeString(char* s) {
Value v;
v.s.t = STRING;
v.s.value = s;
return v;
}
Value sum(Value a, Value b) {
return MakeInt(a.i.value + b.i.value);
}
Value product(Value a, Value b) {
return MakeInt(a.i.value * b.i.value);
}
Value difference(Value a, Value b) {
return MakeInt(a.i.value - b.i.value);
}
Value display(Value v) {
switch(v.t) {
case INT:
printf("%d\n",v.i.value);
break;
case FLOAT:
printf("%f\n",v.f.value);
break;
case STRING:
printf("%s\n",v.s.value);
break;
}
return v;
}
Value equal(Value a, Value b) {
if (a.t != b.t) return MakeInt(1);
switch(a.t) {
case INT: return MakeInt(a.i.value == b.i.value);
case FLOAT: return MakeInt(a.f.value == b.f.value);
case STRING: return MakeInt(!strcmp(a.s.value, b.s.value));
}
return MakeInt(0);
}