Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Integers are now double floating points

  • Loading branch information...
commit 7b98b11c3efd42d012f6ea28ee5e007685a188b8 1 parent 98bd0cc
@txus authored
View
14 README.md
@@ -26,7 +26,7 @@ doing](http://static2.fjcdn.com/comments/I+have+no+idea+what+I+m+doing+thread+_0
In TerrorVM, everything is an object, and every object may have a prototype. The
basic value types that the VM provides are:
-* `Integer`: Simple integers.
+* `Number`: Simple double-precision floating point numbers.
* `String`: Immutable strings.
* `Vector`: Dynamically sized vectors that may contain any type.
* `Map`: Hashmaps (for now only strings are supported as keys).
@@ -56,7 +56,7 @@ the VM types like this:
{
:object => Object,
:vector => Vector,
- :integer => Integer,
+ :number => Number,
...
}
```
@@ -90,10 +90,10 @@ VM.types[:object].clone = VM.primitives[:clone]
VM.types[:object].print = VM.primitives[:print]
VM.types[:object].puts = VM.primitives[:puts]
-VM.types[:integer][:+] = VM.primitives[:'integer_+']
-VM.types[:integer][:-] = VM.primitives[:'integer_-']
-VM.types[:integer][:/] = VM.primitives[:'integer_/']
-VM.types[:integer][:*] = VM.primitives[:'integer_*']
+VM.types[:number][:+] = VM.primitives[:'number_+']
+VM.types[:number][:-] = VM.primitives[:'number_-']
+VM.types[:number][:/] = VM.primitives[:'number_/']
+VM.types[:number][:*] = VM.primitives[:'number_*']
VM.types[:vector][:[]] = VM.primitives[:'vector_[]']
VM.types[:vector][:to_map] = VM.primitives[:vector_to_map]
@@ -165,7 +165,7 @@ these instructions count
as well, so we're in a total of 8.
Right after these counts, we have the literals, each one in its own line. There
-are two kinds of literals: integers and strings. Integers are just numbers, but
+are two kinds of literals: numbers and strings. Numbers are just numbers, but
strings must be preceded by a `"`.
And finally we get to eight lines of numbers, namely the instructions and their
View
BIN  bin/tvm
Binary file not shown
View
2  compiler/examples/numbers.rb
@@ -1 +1 @@
-puts (3-1) + (3 * (9/3))
+puts (3-1) + (3 * (9.0/3.2))
View
1  compiler/hey.rb
@@ -1 +0,0 @@
-puts 'hello world'
View
8 compiler/kernel/prelude.rb
@@ -2,10 +2,10 @@
VM.types[:object].print = VM.primitives[:print]
VM.types[:object].puts = VM.primitives[:puts]
-VM.types[:integer][:+] = VM.primitives[:'integer_+']
-VM.types[:integer][:-] = VM.primitives[:'integer_-']
-VM.types[:integer][:/] = VM.primitives[:'integer_/']
-VM.types[:integer][:*] = VM.primitives[:'integer_*']
+VM.types[:number][:+] = VM.primitives[:'number_+']
+VM.types[:number][:-] = VM.primitives[:'number_-']
+VM.types[:number][:/] = VM.primitives[:'number_/']
+VM.types[:number][:*] = VM.primitives[:'number_*']
VM.types[:vector][:[]] = VM.primitives[:'vector_[]']
VM.types[:vector][:to_map] = VM.primitives[:vector_to_map]
View
4 compiler/lib/terror/visitor.rb
@@ -19,6 +19,10 @@ def fixnum_literal(node, parent)
g.push node.value
end
+ def float_literal(node, parent)
+ g.push node.value
+ end
+
def local_variable_assignment(node, parent)
node.value.lazy_visit self, node
g.setlocal node.name
View
6 compiler/test/terror/visitor_test.rb
@@ -31,7 +31,7 @@ def compiles(code, &block)
end
it 'remembers local names' do
- compiles("a = 3; a = 4") do
+ compiles("a = 3; a = 4.3") do
_push 0
_setlocal 0
_push 1
@@ -188,8 +188,8 @@ def compiles(code, &block)
_setslot 2
_pushlocal 0
- _push 1
- _send 2, 1
+ _push 2
+ _send 3, 1
end
end
end
View
11 examples/numbers.tvm
@@ -1,9 +1,10 @@
_main
-:8:28
+:9:28
3
1
"-
-9
+9.0
+3.2
"/
"*
"+
@@ -21,10 +22,7 @@ _main
17 PUSH
3
17 PUSH
-0
-128 SEND
4
-1
128 SEND
5
1
@@ -34,6 +32,9 @@ _main
128 SEND
7
1
+128 SEND
+8
+1
20 PUSHNIL
144 RET
View
10 kernel/prelude.tvm
@@ -8,14 +8,14 @@ _main
"clone
"print
"puts
-"integer
-"integer_+
+"number
+"number_+
"+
-"integer_-
+"number_-
"-
-"integer_/
+"number_/
"/
-"integer_*
+"number_*
"*
"vector
"vector_[]
View
14 src/terror/bootstrap.c
@@ -5,7 +5,7 @@
#include <terror/primitives.h>
#include <dirent.h>
-VALUE Integer_bp;
+VALUE Number_bp;
VALUE String_bp;
VALUE Vector_bp;
VALUE Map_bp;
@@ -51,11 +51,11 @@ expose_VM(VALUE lobby)
DEFPRIM(primitives, "vector_[]", Primitive_Vector_at);
DEFPRIM(primitives, "vector_to_map", Primitive_Vector_to_map);
- // Integer
- DEFPRIM(primitives, "integer_+", Primitive_Integer_add);
- DEFPRIM(primitives, "integer_-", Primitive_Integer_sub);
- DEFPRIM(primitives, "integer_*", Primitive_Integer_mul);
- DEFPRIM(primitives, "integer_/", Primitive_Integer_div);
+ // Number
+ DEFPRIM(primitives, "number_+", Primitive_Number_add);
+ DEFPRIM(primitives, "number_-", Primitive_Number_sub);
+ DEFPRIM(primitives, "number_*", Primitive_Number_mul);
+ DEFPRIM(primitives, "number_/", Primitive_Number_div);
Value_set(vm, "primitives", Map_new(primitives));
@@ -63,7 +63,7 @@ expose_VM(VALUE lobby)
DArray *types = DArray_create(sizeof(VALUE), 10);
DEFVALUE(types, "object", Object_bp);
- DEFVALUE(types, "integer", Integer_bp);
+ DEFVALUE(types, "number", Number_bp);
DEFVALUE(types, "string", String_bp);
DEFVALUE(types, "vector", Vector_bp);
DEFVALUE(types, "map", Map_bp);
View
2  src/terror/input_reader.c
@@ -84,7 +84,7 @@ parse_string(bstring buf, BytecodeFile *file)
DArray_push(literals, String_new(bdata(bstrcpy(strData))));
bdestroy(strData);
} else {
- DArray_push(literals, Integer_new(atoi(bdata(*line))));
+ DArray_push(literals, Number_new(atof(bdata(*line))));
}
line++; cnt++;
}
View
38 src/terror/primitives.c
@@ -64,57 +64,57 @@ Primitive_require(STATE, void *a, void *_, void *__)
}
/*
- * Integer primitives
+ * Number primitives
*/
VALUE
-Primitive_Integer_add(STATE, void *a, void *b, void *_)
+Primitive_Number_add(STATE, void *a, void *b, void *_)
{
VALUE left = (VALUE)a;
VALUE right = (VALUE)b;
- CHECK_TYPE(left, IntegerType);
- CHECK_TYPE(right, IntegerType);
+ CHECK_TYPE(left, NumberType);
+ CHECK_TYPE(right, NumberType);
- return Integer_new(VAL2INT(left) + VAL2INT(right));
+ return Number_new(VAL2NUM(left) + VAL2NUM(right));
}
VALUE
-Primitive_Integer_sub(STATE, void *a, void *b, void *_)
+Primitive_Number_sub(STATE, void *a, void *b, void *_)
{
VALUE left = (VALUE)a;
VALUE right = (VALUE)b;
- CHECK_TYPE(left, IntegerType);
- CHECK_TYPE(right, IntegerType);
+ CHECK_TYPE(left, NumberType);
+ CHECK_TYPE(right, NumberType);
- return Integer_new(VAL2INT(left) - VAL2INT(right));
+ return Number_new(VAL2NUM(left) - VAL2NUM(right));
}
VALUE
-Primitive_Integer_mul(STATE, void *a, void *b, void *_)
+Primitive_Number_mul(STATE, void *a, void *b, void *_)
{
VALUE left = (VALUE)a;
VALUE right = (VALUE)b;
- CHECK_TYPE(left, IntegerType);
- CHECK_TYPE(right, IntegerType);
+ CHECK_TYPE(left, NumberType);
+ CHECK_TYPE(right, NumberType);
- return Integer_new(VAL2INT(left) * VAL2INT(right));
+ return Number_new(VAL2NUM(left) * VAL2NUM(right));
}
VALUE
-Primitive_Integer_div(STATE, void *a, void *b, void *_)
+Primitive_Number_div(STATE, void *a, void *b, void *_)
{
VALUE left = (VALUE)a;
VALUE right = (VALUE)b;
- CHECK_TYPE(left, IntegerType);
- CHECK_TYPE(right, IntegerType);
+ CHECK_TYPE(left, NumberType);
+ CHECK_TYPE(right, NumberType);
- assert(VAL2INT(right) != 0 && "Cannot divide by zero");
+ assert(VAL2NUM(right) != 0 && "Cannot divide by zero");
- return Integer_new(VAL2INT(left) / VAL2INT(right));
+ return Number_new(VAL2NUM(left) / VAL2NUM(right));
}
/*
@@ -128,7 +128,7 @@ Primitive_Vector_at(STATE, void *a, void *b, void *_)
VALUE index = (VALUE)b;
CHECK_TYPE(vector, VectorType);
- CHECK_TYPE(index, IntegerType);
+ CHECK_TYPE(index, NumberType);
VALUE result = (VALUE)DArray_at(VAL2ARY(vector), VAL2INT(index));
View
10 src/terror/primitives.h
@@ -15,11 +15,11 @@ VALUE Primitive_print(PRIMITIVE_ARGS);
VALUE Primitive_puts(PRIMITIVE_ARGS);
VALUE Primitive_require(PRIMITIVE_ARGS);
-// Integer primitive methods
-VALUE Primitive_Integer_add(PRIMITIVE_ARGS);
-VALUE Primitive_Integer_sub(PRIMITIVE_ARGS);
-VALUE Primitive_Integer_mul(PRIMITIVE_ARGS);
-VALUE Primitive_Integer_div(PRIMITIVE_ARGS);
+// Number primitive methods
+VALUE Primitive_Number_add(PRIMITIVE_ARGS);
+VALUE Primitive_Number_sub(PRIMITIVE_ARGS);
+VALUE Primitive_Number_mul(PRIMITIVE_ARGS);
+VALUE Primitive_Number_div(PRIMITIVE_ARGS);
// Vector primitive methods
VALUE Primitive_Vector_at(PRIMITIVE_ARGS);
View
8 src/terror/runtime.c
@@ -5,7 +5,7 @@
// Blueprints
VALUE Object_bp = NULL;
-VALUE Integer_bp = NULL;
+VALUE Number_bp = NULL;
VALUE String_bp = NULL;
VALUE Vector_bp = NULL;
VALUE Map_bp = NULL;
@@ -25,7 +25,7 @@ void Runtime_init() {
DEFNATIVE(Object_bp, "[]", Primitive_Map_get);
DEFNATIVE(Object_bp, "[]=", Primitive_Map_set);
- Integer_bp = Value_from_prototype(IntegerType, Object_bp);
+ Number_bp = Value_from_prototype(NumberType, Object_bp);
String_bp = Value_from_prototype(StringType, Object_bp);
Vector_bp = Value_from_prototype(VectorType, Object_bp);
Map_bp = Value_from_prototype(MapType, Object_bp);
@@ -38,13 +38,13 @@ void Runtime_init() {
}
void Runtime_destroy() {
- Value_destroy(Integer_bp);
+ Value_destroy(Number_bp);
Value_destroy(String_bp);
Value_destroy(Vector_bp);
Value_destroy(Map_bp);
Value_destroy(Closure_bp);
Value_destroy(Object_bp);
- Integer_bp = NULL;
+ Number_bp = NULL;
String_bp = NULL;
Vector_bp = NULL;
Map_bp = NULL;
View
2  src/terror/runtime.h
@@ -4,7 +4,7 @@
#include <terror/value.h>
extern VALUE Object_bp;
-extern VALUE Integer_bp;
+extern VALUE Number_bp;
extern VALUE String_bp;
extern VALUE Vector_bp;
extern VALUE Map_bp;
View
14 src/terror/value.c
@@ -6,7 +6,7 @@
#include <terror/gc.h>
VALUE Object_bp;
-VALUE Integer_bp;
+VALUE Number_bp;
VALUE String_bp;
VALUE Vector_bp;
VALUE Map_bp;
@@ -56,7 +56,7 @@ __Value_print(VALUE o)
// Blueprints
if(o == Object_bp) { printf("Object"); return;
- } else if (o == Integer_bp) { printf("Integer"); return;
+ } else if (o == Number_bp) { printf("Number"); return;
} else if (o == String_bp) { printf("String"); return;
} else if (o == Closure_bp) { printf("Closure"); return;
} else if (o == Vector_bp) { printf("Vector"); return;
@@ -64,8 +64,8 @@ __Value_print(VALUE o)
}
switch(o->type) {
- case IntegerType: {
- printf("%i", VAL2INT(o));
+ case NumberType: {
+ printf("%f", VAL2NUM(o));
break;
}
case StringType: {
@@ -131,10 +131,10 @@ Lobby_new()
}
VALUE
-Integer_new(int num)
+Number_new(double num)
{
- VALUE val = Value_from_prototype(IntegerType, Integer_bp);
- val->data.as_int = num;
+ VALUE val = Value_from_prototype(NumberType, Number_bp);
+ val->data.as_num = num;
return val;
}
View
7 src/terror/value.h
@@ -10,7 +10,7 @@ struct val_s {
GCHeader gc;
ValueType type;
union {
- int as_int;
+ double as_num;
char *as_str;
void *as_data;
} data;
@@ -27,8 +27,9 @@ void Value_destroy(VALUE);
void Value_print(VALUE);
VALUE Lobby_new();
-VALUE Integer_new(int);
-#define VAL2INT(o) (o->data.as_int)
+VALUE Number_new(double);
+#define VAL2NUM(o) (o->data.as_num)
+#define VAL2INT(o) ((int)(o->data.as_num))
VALUE String_new(char*);
#define VAL2STR(o) (o->data.as_str)
View
2  src/terror/value_type.h
@@ -2,7 +2,7 @@
#define _tvm_value_type_h_
typedef enum {
- IntegerType = 0,
+ NumberType = 0,
ObjectType,
StringType,
VectorType,
View
40 src/terror/vm.c
@@ -217,34 +217,22 @@ VALUE VM_run(STATE)
break;
}
- /* if(op2 == 1 && strcmp(VAL2STR(name), "[]") == 0) { // getslot */
- /* VALUE key = (VALUE)DArray_at(locals, 0); */
- /* Stack_push(STACK, Value_get(receiver, VAL2STR(key))); */
- /* break; */
- /* } */
-
- /* if(op2 == 2 && strcmp(VAL2STR(name), "[]=") == 0) { // setslot */
- /* VALUE key = (VALUE)DArray_at(locals, 0); */
- /* VALUE value = (VALUE)DArray_at(locals, 1); */
- /* Value_set(receiver, VAL2STR(key), value); */
- /* Stack_push(STACK, value); */
- /* break; */
- /* } */
-
- if(closure->type == StringType) {
- printf("BUG\n");
-
- assert(receiver == Integer_bp && "Not the droids we're looking for");
- VALUE cls = Value_get(receiver, "[]");
- printf("SEND: Integer#[]\n\t");
- Value_print(cls);
- printf("\n");
-
- printf("Calling %s on ", VAL2STR(name));
- Value_print(receiver);
- printf(". The closure is a string: %s.\n", VAL2STR(closure));
+#ifdef OPTIMIZE_SEND
+ if(op2 == 1 && strcmp(VAL2STR(name), "[]") == 0) { // getslot
+ VALUE key = (VALUE)DArray_at(locals, 0);
+ Stack_push(STACK, Value_get(receiver, VAL2STR(key)));
+ break;
}
+ if(op2 == 2 && strcmp(VAL2STR(name), "[]=") == 0) { // setslot
+ VALUE key = (VALUE)DArray_at(locals, 0);
+ VALUE value = (VALUE)DArray_at(locals, 1);
+ Value_set(receiver, VAL2STR(key), value);
+ Stack_push(STACK, value);
+ break;
+ }
+#endif
+
state->ret = ip; // save where we want to return
ip = Function_call(state, VAL2FN(closure), receiver, locals);
ip--; // because we increment after each while cycle
View
5 tests/input_reader_tests.c
@@ -12,8 +12,13 @@ char *test_load()
Function *add = Hashmap_get(file->functions, bfromcstr("add"));
mu_assert(*main->code == PUSH, "error parsing main");
+
VALUE first_lit = DArray_first(main->literals);
mu_assert(strcmp(VAL2STR(first_lit), "add") == 0, "error parsing literal in main");
+
+ VALUE first_num = DArray_at(main->literals, 1);
+ mu_assert(VAL2NUM(first_num) == 1.2, "error parsing float literal in main");
+
mu_assert(*add->code == PUSHSELF, "error parsing add");
return NULL;
View
2  tests/testfile.tvm
@@ -1,7 +1,7 @@
_main
:3:9
"add
-1
+1.2
4
17 PUSHINT
1
View
20 tests/value_tests.c
@@ -4,10 +4,10 @@
char *test_integer_new()
{
- VALUE intval = Integer_new(123);
+ VALUE intval = Number_new(123);
- mu_assert(intval->type == IntegerType, "failed assigning type");
- mu_assert(VAL2INT(intval) == 123, "failed assigning integer value");
+ mu_assert(intval->type == NumberType, "failed assigning type");
+ mu_assert(VAL2NUM(intval) == 123, "failed assigning integer value");
return NULL;
}
@@ -62,7 +62,7 @@ char *test_lobby_new()
char *test_destroy()
{
- VALUE obj = Integer_new(123);
+ VALUE obj = Number_new(123);
Value_destroy(obj);
mu_assert(!obj->type, "failed destroying object");
@@ -71,17 +71,17 @@ char *test_destroy()
char *test_get()
{
- VALUE obj = Integer_new(123);
- Value_set(obj, "foo", Integer_new(99));
+ VALUE obj = Number_new(123);
+ Value_set(obj, "foo", Number_new(99));
VALUE number = Value_get(obj, "foo");
- mu_assert(number->type == IntegerType, "failed getting member of integer");
+ mu_assert(number->type == NumberType, "failed getting member of integer");
return NULL;
}
char *test_get_undefined()
{
- VALUE obj = Integer_new(123);
+ VALUE obj = Number_new(123);
VALUE closure = Value_get(obj, "foo");
mu_assert(closure == NULL, "expected NULL, got something");
@@ -90,8 +90,8 @@ char *test_get_undefined()
char *test_set()
{
- VALUE obj = Integer_new(123);
- VALUE integer = Integer_new(99);
+ VALUE obj = Number_new(123);
+ VALUE integer = Number_new(99);
Value_set(obj, "foo", integer);
mu_assert(Value_get(obj, "foo") == integer, "failed assigning foo");
View
18 tests/vm_tests.c
@@ -69,7 +69,7 @@ char *test_push()
{
SETUP();
- PUSH_LITERAL(Integer_new(123), integer);
+ PUSH_LITERAL(Number_new(123), integer);
PUSH_LITERAL(String_new("foo"), string);
RUN(
@@ -130,7 +130,7 @@ char *test_pushlocal()
{
SETUP();
- PUSH_LOCAL(Integer_new(123), integer);
+ PUSH_LOCAL(Number_new(123), integer);
RUN(
PUSHLOCAL, 0,
@@ -276,7 +276,7 @@ char *test_send()
RET
);
- PUSH_LITERAL(Integer_new(1), a);
+ PUSH_LITERAL(Number_new(1), a);
PUSH_LITERAL(String_new("echo"), method);
RUN(
@@ -290,7 +290,7 @@ char *test_send()
RET
);
- mu_assert(VAL2INT(result) == 1, "Send failed.");
+ mu_assert(VAL2NUM(result) == 1, "Send failed.");
TEARDOWN();
}
@@ -299,7 +299,7 @@ char *test_send_getslot()
{
SETUP();
- PUSH_LITERAL(Integer_new(1), a);
+ PUSH_LITERAL(Number_new(1), a);
PUSH_LITERAL(String_new("value"), slot);
RUN(
@@ -313,7 +313,7 @@ char *test_send_getslot()
RET
)
- mu_assert(VAL2INT(result) == 1, "Send didn't fall back to getslot.");
+ mu_assert(VAL2NUM(result) == 1, "Send didn't fall back to getslot.");
TEARDOWN();
}
@@ -328,20 +328,20 @@ char *test_send_apply()
RET
);
- PUSH_LITERAL(Integer_new(1), a);
+ PUSH_LITERAL(Number_new(1), a);
PUSH_LITERAL(String_new("echo"), method);
PUSH_LITERAL(String_new("apply"), apply);
RUN(
DEFN, 1,
- // self == closure, method = apply, first arg == Integer(1)
+ // self == closure, method = apply, first arg == Number(1)
PUSH, 0,
SEND, 2, 1,
RET
);
- mu_assert(VAL2INT(result) == 1, "Send apply failed.");
+ mu_assert(VAL2NUM(result) == 1, "Send apply failed.");
TEARDOWN();
}
Please sign in to comment.
Something went wrong with that request. Please try again.