Permalink
Browse files

Integers are now double floating points

  • Loading branch information...
1 parent 98bd0cc commit 7b98b11c3efd42d012f6ea28ee5e007685a188b8 @txus committed Dec 30, 2012
View
@@ -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.
@@ -1 +1 @@
-puts (3-1) + (3 * (9/3))
+puts (3-1) + (3 * (9.0/3.2))
View
@@ -1 +0,0 @@
-puts 'hello world'
@@ -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]
@@ -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
@@ -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
@@ -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
@@ -8,14 +8,14 @@ _main
"clone
"print
"puts
-"integer
-"integer_+
+"number
+"number_+
"+
-"integer_-
+"number_-
"-
-"integer_/
+"number_/
"/
-"integer_*
+"number_*
"*
"vector
"vector_[]
View
@@ -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,19 +51,19 @@ 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));
// VM.types map
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);
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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,16 +56,16 @@ __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;
} else if (o == Map_bp) { printf("Map"); return;
}
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;
}
Oops, something went wrong.

0 comments on commit 7b98b11

Please sign in to comment.