enum Type
{
BOOLEAN,
+ CLOSURE,
INTEGER,
STRING
};
union Instance
{
bool boolean;
+ Object (*closure)(size_t, Object*);
int32_t integer;
const char* string;
};
}
// TODO Make this conditionally added
-Object builtin$negate(Object input)
+Object operator$negate(Object input)
{
assert(input.type == INTEGER);
return result;
}
-Object builtin$add(Object left, Object right)
+Object operator$add(Object left, Object right)
{
assert(left.type == INTEGER);
assert(right.type == INTEGER);
return result;
}
-Object builtin$subtract(Object left, Object right)
+Object operator$subtract(Object left, Object right)
{
assert(left.type == INTEGER);
assert(right.type == INTEGER);
return result;
}
-Object builtin$multiply(Object left, Object right)
+Object operator$multiply(Object left, Object right)
{
assert(left.type == INTEGER);
assert(right.type == INTEGER);
return result;
}
-Object builtin$integerDivide(Object left, Object right)
+Object operator$integerDivide(Object left, Object right)
{
assert(left.type == INTEGER);
assert(right.type == INTEGER);
return result;
}
-Object builtin$modularDivide(Object left, Object right)
+Object operator$modularDivide(Object left, Object right)
{
assert(left.type == INTEGER);
assert(right.type == INTEGER);
return result;
}
-Object builtin$equals(Object left, Object right)
+Object operator$equals(Object left, Object right)
{
assert(left.type == INTEGER);
assert(right.type == INTEGER);
return result;
}
-Object builtin$notEquals(Object left, Object right)
+Object operator$notEquals(Object left, Object right)
{
assert(left.type == INTEGER);
assert(right.type == INTEGER);
return result;
}
-Object builtin$greaterThan(Object left, Object right)
+Object operator$greaterThan(Object left, Object right)
{
assert(left.type == INTEGER);
assert(right.type == INTEGER);
return result;
}
-Object builtin$lessThan(Object left, Object right)
+Object operator$lessThan(Object left, Object right)
{
assert(left.type == INTEGER);
assert(right.type == INTEGER);
return result;
}
-Object builtin$greaterThanOrEqual(Object left, Object right)
+Object operator$greaterThanOrEqual(Object left, Object right)
{
assert(left.type == INTEGER);
assert(right.type == INTEGER);
return result;
}
-Object builtin$lessThanOrEqual(Object left, Object right)
+Object operator$lessThanOrEqual(Object left, Object right)
{
assert(left.type == INTEGER);
assert(right.type == INTEGER);
return result;
}
-Object builtin$and(Object left, Object right)
+Object operator$and(Object left, Object right)
{
assert(left.type == BOOLEAN);
assert(right.type == BOOLEAN);
return result;
}
-Object builtin$or(Object left, Object right)
+Object operator$or(Object left, Object right)
{
assert(left.type == BOOLEAN);
assert(right.type == BOOLEAN);
}
{% if 'pow' in builtins %}
-Object builtin$pow(Object base, Object exponent)
+Object builtin$pow$implementation(size_t argc, Object* args)
{
+ assert(argc == 2);
+
+ Object base = args[0];
+ Object exponent = args[1];
+
assert(base.type == INTEGER);
assert(exponent.type == INTEGER);
result.instance.integer = pow(base.instance.integer, exponent.instance.integer);
return result;
}
+
+Object builtin$pow = { CLOSURE, (Instance)builtin$pow$implementation };
{% endif %}
{% if 'print' in builtins %}
-void builtin$print(Object output)
+Object builtin$print$implementation(size_t argc, Object* args)
{
- switch(output.type)
+ for(size_t i = 0; i < argc; i++)
{
- case BOOLEAN:
- fputs(output.instance.boolean ? "true" : "false", stdout);
- break;
+ Object output = args[i];
+ switch(output.type)
+ {
+ case BOOLEAN:
+ fputs(output.instance.boolean ? "true" : "false", stdout);
+ break;
- case INTEGER:
- printf("%" PRId32, output.instance.integer);
- break;
+ case INTEGER:
+ printf("%" PRId32, output.instance.integer);
+ break;
- case STRING:
- // Using fwrite instead of printf to handle size_t length
- printf("%s", output.instance.string);
- break;
+ case STRING:
+ // Using fwrite instead of printf to handle size_t length
+ printf("%s", output.instance.string);
+ break;
- default:
- assert(false);
+ default:
+ assert(false);
+ }
}
+
+ // TODO Return something better
+ return FALSE;
}
+
+Object builtin$print = { CLOSURE, (Instance)builtin$print$implementation };
{% endif %}
int main(int argc, char** argv)