Skip to content

Commit

Permalink
upvalues
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusandra committed Aug 21, 2024
1 parent 8221c7b commit c34ef5d
Show file tree
Hide file tree
Showing 27 changed files with 540 additions and 216 deletions.
2 changes: 1 addition & 1 deletion hogvm/__tests__/0.hog
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// TODO: implement closures
// TODO: closing of upvalues

//fn returnCallable(a) {
// return x -> x * a
Expand Down
4 changes: 3 additions & 1 deletion hogvm/__tests__/__snapshots__/0.hoge
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
["_h"]
["_h", 33, 3, 52, "outer", 0, 1, 18, 55, 0, 2, "print", 1, 35, 33, 4, 56, 0, 55, 0, 2, "print", 1, 35, 31, 38, 53, 1,
true, 0, 52, "inner", 0, 2, 19, 55, 0, 2, "print", 1, 35, 55, 1, 54, 0, 35, 55, 0, 2, "print", 1, 35, 31, 38, 53, 2,
true, 0, true, 1, 36, 0, 2, "print", 1, 35, 36, 2, 54, 0, 35, 36, 0, 2, "print", 1, 35, 35, 35, 35]
6 changes: 6 additions & 0 deletions hogvm/__tests__/__snapshots__/0.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
3
3
3
4
4
4
6 changes: 3 additions & 3 deletions hogvm/__tests__/__snapshots__/catch.hoge
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
["_h", 52, "FishError", 1, 8, 36, 0, 32, "FishError", 2, "HogError", 2, 38, 53, 52, "FoodError", 1, 8, 36, 0, 32,
"FoodError", 2, "HogError", 2, 38, 53, 50, 11, 32, "You forgot to feed your fish", 36, 0, 54, 1, 49, 51, 39, 55, 36, 2,
32, "type", 45, 32, "FoodError", 36, 3, 11, 40, 16, 36, 2, 32, "message", 45, 32, "Problem with your food: ", 2,
["_h", 52, "FishError", 1, 0, 8, 36, 0, 32, "FishError", 2, "HogError", 2, 38, 53, 0, 52, "FoodError", 1, 0, 8, 36, 0,
32, "FoodError", 2, "HogError", 2, 38, 53, 0, 50, 11, 32, "You forgot to feed your fish", 36, 0, 54, 1, 49, 51, 39, 55,
36, 2, 32, "type", 45, 32, "FoodError", 36, 3, 11, 40, 16, 36, 2, 32, "message", 45, 32, "Problem with your food: ", 2,
"concat", 2, 2, "print", 1, 35, 39, 25, 32, "FishError", 36, 3, 11, 40, 16, 36, 2, 32, "message", 45, 32,
"Problem with your fish: ", 2, "concat", 2, 2, "print", 1, 35, 39, 2, 35, 49, 35, 35, 50, 11, 32,
"Your fish are hungry", 36, 1, 54, 1, 49, 51, 39, 55, 36, 2, 32, "type", 45, 32, "FoodError", 36, 3, 11, 40, 16, 36, 2,
Expand Down
9 changes: 8 additions & 1 deletion hogvm/__tests__/__snapshots__/exceptions.hoge
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,11 @@
35, 50, 14, 32, "try again", 2, "print", 1, 35, 2, "Error", 0, 49, 51, 39, 22, 36, 0, 32, "type", 45, 32,
" was the exception", 36, 0, 2, "concat", 2, 2, "print", 1, 35, 39, 2, 35, 49, 35, 35, 32, "------------------", 2,
"print", 1, 35, 50, 14, 32, "try again", 2, "print", 1, 35, 2, "Error", 0, 49, 51, 39, 17, 36, 0, 32, "type", 45, 32,
"No var for error", 2, "print", 1, 35, 39, 2, 35, 49, 35, 35, 32, "------------------", 2, "print", 1, 35]
"No var for error", 2, "print", 1, 35, 39, 2, 35, 49, 35, 35, 32, "------------------", 2, "print", 1, 35, 52, "third",
0, 0, 14, 32, "Throwing in third", 2, "print", 1, 35, 32, "Threw in third", 2, "Error", 1, 49, 31, 38, 53, 0, 52,
"second", 0, 1, 13, 32, "second", 2, "print", 1, 35, 55, 0, 54, 0, 35, 31, 38, 53, 1, true, 0, 52, "first", 0, 1, 13,
32, "first", 2, "print", 1, 35, 55, 0, 54, 0, 35, 31, 38, 53, 1, true, 1, 52, "base", 0, 1, 43, 32, "base", 2, "print",
1, 35, 50, 9, 55, 0, 54, 0, 35, 51, 39, 25, 36, 0, 32, "type", 45, 36, 0, 32, "Caught in base: ", 2, "concat", 2, 2,
"print", 1, 35, 36, 0, 49, 39, 2, 35, 49, 35, 35, 31, 38, 53, 1, true, 2, 50, 9, 36, 3, 54, 0, 35, 51, 39, 22, 36, 4,
32, "type", 45, 36, 4, 32, "Caught in root: ", 2, "concat", 2, 2, "print", 1, 35, 39, 2, 35, 49, 35, 35, 32, "The end",
2, "print", 1, 35, 32, "------------------", 2, "print", 1, 35, 35, 35, 35, 35]
8 changes: 8 additions & 0 deletions hogvm/__tests__/__snapshots__/exceptions.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,11 @@ Error('An error occurred') was the exception
try again
No var for error
------------------
base
first
second
Throwing in third
Caught in base: Error('Threw in third')
Caught in root: Error('Threw in third')
The end
------------------
13 changes: 8 additions & 5 deletions hogvm/__tests__/__snapshots__/functionVars.hoge
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
["_h", 52, "execFunction", 0, 8, 32, "execFunction", 2, "print", 1, 35, 31, 38, 53, 52, "execFunctionNested", 0, 27, 52,
"execFunction", 0, 8, 32, "execFunctionNew", 2, "print", 1, 35, 31, 38, 53, 32, "execFunctionNested", 2, "print", 1, 35,
36, 0, 54, 0, 35, 31, 38, 35, 53, 36, 0, 54, 0, 35, 36, 1, 54, 0, 35, 36, 0, 54, 0, 35, 32, "--------", 2, "print", 1,
35, 52, "lambda", 1, 6, 33, 2, 36, 0, 8, 38, 53, 36, 2, 2, "print", 1, 35, 33, 2, 36, 2, 54, 1, 2, "print", 1, 35, 33,
8, 36, 2, 54, 1, 2, "print", 1, 35, 35, 35, 35]
["_h", 52, "execFunction", 0, 0, 8, 32, "execFunction", 2, "print", 1, 35, 31, 38, 53, 0, 52, "execFunctionNested", 0,
0, 29, 52, "execFunction", 0, 0, 8, 32, "execFunctionNew", 2, "print", 1, 35, 31, 38, 53, 0, 32, "execFunctionNested",
2, "print", 1, 35, 36, 0, 54, 0, 35, 31, 38, 35, 53, 0, 36, 0, 54, 0, 35, 36, 1, 54, 0, 35, 36, 0, 54, 0, 35, 32,
"--------", 2, "print", 1, 35, 52, "secondExecFunction", 0, 0, 8, 32, "secondExecFunction", 2, "print", 1, 35, 31, 38,
53, 0, 52, "secondExecFunctionNested", 0, 1, 13, 32, "secondExecFunctionNested", 2, "print", 1, 35, 55, 0, 54, 0, 35,
31, 38, 53, 1, true, 2, 36, 2, 54, 0, 35, 36, 3, 54, 0, 35, 36, 2, 54, 0, 35, 32, "--------", 2, "print", 1, 35, 52,
"lambda", 1, 0, 6, 33, 2, 36, 0, 8, 38, 53, 0, 36, 4, 2, "print", 1, 35, 33, 2, 36, 4, 54, 1, 2, "print", 1, 35, 33, 8,
36, 4, 54, 1, 2, "print", 1, 35, 35, 35, 35, 35, 35]
7 changes: 6 additions & 1 deletion hogvm/__tests__/__snapshots__/functionVars.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ execFunctionNested
execFunctionNew
execFunction
--------
{'__hogClosure__': true, 'callable': {'__hogCallable__': 'local', 'argCount': 1, 'ip': 70, 'name': 'lambda'}, 'captured': []}
secondExecFunction
secondExecFunctionNested
secondExecFunction
secondExecFunction
--------
{'__hogClosure__': true, 'callable': {'__hogCallable__': 'local', 'argCount': 1, 'upvalueCount': 0, 'ip': 136, 'name': 'lambda'}, 'upvalues': []}
4
16
28 changes: 14 additions & 14 deletions hogvm/__tests__/__snapshots__/functions.hoge
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
["_h", 32, "-- test functions --", 2, "print", 1, 35, 52, "add", 2, 6, 36, 0, 36, 1, 6, 38, 53, 52, "add2", 2, 9, 36, 0,
36, 1, 6, 36, 2, 38, 35, 53, 52, "mult", 2, 6, 36, 0, 36, 1, 8, 38, 53, 52, "noArgs", 0, 12, 32, "basdfasdf", 33, 3, 33,
2, 6, 36, 1, 38, 35, 35, 53, 52, "empty", 0, 2, 31, 38, 53, 52, "empty2", 0, 2, 31, 38, 53, 52, "empty3", 0, 2, 31, 38,
53, 52, "noReturn", 0, 14, 33, 1, 33, 2, 36, 1, 36, 0, 6, 31, 38, 35, 35, 35, 53, 52, "emptyReturn", 0, 2, 31, 38, 53,
52, "emptyReturnBeforeOtherStuff", 0, 10, 31, 38, 33, 2, 33, 2, 6, 35, 31, 38, 53, 52,
"emptyReturnBeforeOtherStuffNoSemicolon", 0, 6, 33, 2, 33, 2, 6, 38, 53, 52, "ifThenReturn", 0, 8, 30, 40, 2, 31, 38,
33, 4, 38, 53, 33, 4, 33, 3, 36, 0, 54, 2, 2, "print", 1, 35, 33, 1, 33, 1, 36, 0, 54, 2, 33, 100, 33, 4, 33, 3, 36, 0,
54, 2, 6, 6, 2, "print", 1, 35, 36, 3, 54, 0, 47, 3, 35, 33, -1, 2, "print", 1, 35, 36, 4, 54, 0, 47, 3, 35, 33, -1, 2,
"print", 1, 35, 36, 5, 54, 0, 47, 3, 35, 33, -1, 2, "print", 1, 35, 36, 6, 54, 0, 47, 3, 35, 33, -1, 2, "print", 1, 35,
36, 7, 54, 0, 47, 3, 35, 33, -1, 2, "print", 1, 35, 36, 8, 54, 0, 47, 3, 35, 33, -1, 2, "print", 1, 35, 36, 9, 54, 0,
47, 3, 35, 33, -1, 2, "print", 1, 35, 36, 10, 54, 0, 47, 3, 35, 33, -1, 2, "print", 1, 35, 36, 11, 54, 0, 47, 3, 35, 33,
-1, 2, "print", 1, 35, 33, 2, 33, 1, 33, 2, 36, 0, 54, 2, 33, 100, 33, 4, 33, 3, 36, 0, 54, 2, 6, 6, 36, 2, 54, 2, 2,
"print", 1, 35, 33, 10, 33, 1, 33, 2, 36, 1, 54, 2, 33, 100, 33, 4, 33, 3, 36, 1, 54, 2, 6, 6, 36, 2, 54, 2, 2, "print",
1, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35]
["_h", 32, "-- test functions --", 2, "print", 1, 35, 52, "add", 2, 0, 6, 36, 0, 36, 1, 6, 38, 53, 0, 52, "add2", 2, 0,
9, 36, 0, 36, 1, 6, 36, 2, 38, 35, 53, 0, 52, "mult", 2, 0, 6, 36, 0, 36, 1, 8, 38, 53, 0, 52, "noArgs", 0, 0, 12, 32,
"basdfasdf", 33, 3, 33, 2, 6, 36, 1, 38, 35, 35, 53, 0, 52, "empty", 0, 0, 2, 31, 38, 53, 0, 52, "empty2", 0, 0, 2, 31,
38, 53, 0, 52, "empty3", 0, 0, 2, 31, 38, 53, 0, 52, "noReturn", 0, 0, 14, 33, 1, 33, 2, 36, 1, 36, 0, 6, 31, 38, 35,
35, 35, 53, 0, 52, "emptyReturn", 0, 0, 2, 31, 38, 53, 0, 52, "emptyReturnBeforeOtherStuff", 0, 0, 10, 31, 38, 33, 2,
33, 2, 6, 35, 31, 38, 53, 0, 52, "emptyReturnBeforeOtherStuffNoSemicolon", 0, 0, 6, 33, 2, 33, 2, 6, 38, 53, 0, 52,
"ifThenReturn", 0, 0, 8, 30, 40, 2, 31, 38, 33, 4, 38, 53, 0, 33, 4, 33, 3, 36, 0, 54, 2, 2, "print", 1, 35, 33, 1, 33,
1, 36, 0, 54, 2, 33, 100, 33, 4, 33, 3, 36, 0, 54, 2, 6, 6, 2, "print", 1, 35, 36, 3, 54, 0, 47, 3, 35, 33, -1, 2,
"print", 1, 35, 36, 4, 54, 0, 47, 3, 35, 33, -1, 2, "print", 1, 35, 36, 5, 54, 0, 47, 3, 35, 33, -1, 2, "print", 1, 35,
36, 6, 54, 0, 47, 3, 35, 33, -1, 2, "print", 1, 35, 36, 7, 54, 0, 47, 3, 35, 33, -1, 2, "print", 1, 35, 36, 8, 54, 0,
47, 3, 35, 33, -1, 2, "print", 1, 35, 36, 9, 54, 0, 47, 3, 35, 33, -1, 2, "print", 1, 35, 36, 10, 54, 0, 47, 3, 35, 33,
-1, 2, "print", 1, 35, 36, 11, 54, 0, 47, 3, 35, 33, -1, 2, "print", 1, 35, 33, 2, 33, 1, 33, 2, 36, 0, 54, 2, 33, 100,
33, 4, 33, 3, 36, 0, 54, 2, 6, 6, 36, 2, 54, 2, 2, "print", 1, 35, 33, 10, 33, 1, 33, 2, 36, 1, 54, 2, 33, 100, 33, 4,
33, 3, 36, 1, 54, 2, 6, 6, 36, 2, 54, 2, 2, "print", 1, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35]
15 changes: 8 additions & 7 deletions hogvm/__tests__/__snapshots__/mandelbrot.hoge
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
["_h", 52, "mandelbrot", 3, 93, 34, 0.0, 34, 0.0, 33, 0, 36, 0, 36, 5, 15, 33, 4, 36, 4, 36, 4, 8, 36, 3, 36, 3, 8, 6,
16, 3, 2, 40, 44, 36, 2, 36, 4, 36, 4, 8, 36, 3, 36, 3, 8, 7, 6, 36, 1, 36, 4, 36, 3, 33, 2, 8, 8, 6, 36, 6, 37, 3, 36,
7, 37, 4, 33, 1, 36, 5, 6, 37, 5, 35, 35, 39, -67, 36, 0, 36, 5, 11, 40, 5, 32, " ", 38, 39, 3, 32, "#", 38, 31, 38, 35,
35, 35, 53, 33, 80, 33, 24, 34, -2.0, 34, 1.0, 34, -1.0, 34, 1.0, 33, 30, 33, 0, 36, 2, 36, 8, 15, 40, 87, 32, "", 33,
0, 36, 1, 36, 10, 15, 40, 59, 36, 3, 36, 3, 36, 4, 7, 36, 1, 36, 10, 9, 8, 6, 36, 5, 36, 5, 36, 6, 7, 36, 2, 36, 8, 9,
8, 6, 36, 7, 36, 12, 36, 11, 36, 0, 54, 3, 36, 13, 36, 9, 2, "concat", 2, 37, 9, 33, 1, 36, 10, 6, 37, 10, 35, 35, 35,
39, -66, 36, 9, 2, "print", 1, 35, 33, 1, 36, 8, 6, 37, 8, 35, 35, 39, -94, 35, 35, 35, 35, 35, 35, 35, 35, 35]
["_h", 52, "mandelbrot", 3, 0, 93, 34, 0.0, 34, 0.0, 33, 0, 36, 0, 36, 5, 15, 33, 4, 36, 4, 36, 4, 8, 36, 3, 36, 3, 8,
6, 16, 3, 2, 40, 44, 36, 2, 36, 4, 36, 4, 8, 36, 3, 36, 3, 8, 7, 6, 36, 1, 36, 4, 36, 3, 33, 2, 8, 8, 6, 36, 6, 37, 3,
36, 7, 37, 4, 33, 1, 36, 5, 6, 37, 5, 35, 35, 39, -67, 36, 0, 36, 5, 11, 40, 5, 32, " ", 38, 39, 3, 32, "#", 38, 31, 38,
35, 35, 35, 53, 0, 52, "main", 0, 1, 120, 33, 80, 33, 24, 34, -2.0, 34, 1.0, 34, -1.0, 34, 1.0, 33, 30, 33, 0, 36, 1,
36, 7, 15, 40, 87, 32, "", 33, 0, 36, 0, 36, 9, 15, 40, 59, 36, 2, 36, 2, 36, 3, 7, 36, 0, 36, 9, 9, 8, 6, 36, 4, 36, 4,
36, 5, 7, 36, 1, 36, 7, 9, 8, 6, 36, 6, 36, 11, 36, 10, 55, 0, 54, 3, 36, 12, 36, 8, 2, "concat", 2, 37, 8, 33, 1, 36,
9, 6, 37, 9, 35, 35, 35, 39, -66, 36, 8, 2, "print", 1, 35, 33, 1, 36, 7, 6, 37, 7, 35, 35, 39, -94, 31, 38, 35, 35, 35,
35, 35, 35, 35, 35, 53, 1, true, 0, 36, 1, 54, 0, 35, 35, 35]
2 changes: 1 addition & 1 deletion hogvm/__tests__/__snapshots__/operations.hoge
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
["_h", 52, "test", 1, 11, 36, 0, 2, "jsonStringify", 1, 2, "print", 1, 35, 31, 38, 53, 32,
["_h", 52, "test", 1, 0, 11, 36, 0, 2, "jsonStringify", 1, 2, "print", 1, 35, 31, 38, 53, 0, 32,
"-- test the most common expressions --", 2, "print", 1, 35, 33, 2, 33, 1, 6, 36, 0, 54, 1, 35, 33, 2, 33, 1, 7, 36, 0,
54, 1, 35, 33, 2, 33, 3, 8, 36, 0, 54, 1, 35, 33, 2, 33, 3, 9, 36, 0, 54, 1, 35, 33, 2, 33, 3, 10, 36, 0, 54, 1, 35, 33,
2, 33, 1, 3, 2, 36, 0, 54, 1, 35, 33, 0, 33, 1, 4, 2, 36, 0, 54, 1, 35, 33, 0, 33, 1, 3, 2, 36, 0, 54, 1, 35, 33, 2, 33,
Expand Down
69 changes: 30 additions & 39 deletions hogvm/__tests__/exceptions.hog
Original file line number Diff line number Diff line change
Expand Up @@ -35,45 +35,36 @@ try {

print('------------------')

// TODO: need closures to access
fn third() {
print('Throwing in third')
throw Error('Threw in third')
}

fn second() {
print('second')
third()
}

fn first() {
print('first')
second()
}

//fn third() {
// print('Throwing in third')
// throw Error('Threw in third')
//}
//
//fn second() {
// print('second')
// third()
//}
//
//fn first() {
// print('first')
// second()
//}
//
//fn base() {
// print('base')
// try {
// first()
// } catch (e) {
// print(f'Caught in base: {e}')
// throw e
// }
//}
//
//try {
// base()
//} catch (e) {
// print(f'Caught in root: {e}')
//}
//print('The end')
fn base() {
print('base')
try {
first()
} catch (e) {
print(f'Caught in base: {e}')
throw e
}
}

//print('------------------')
try {
base()
} catch (e) {
print(f'Caught in root: {e}')
}
print('The end')

// TODO: throw undefined global access in Hog
//try {
// print({key: 'value'})
//} catch(e) {
// print(e)
//}
print('------------------')
30 changes: 14 additions & 16 deletions hogvm/__tests__/functionVars.hog
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,20 @@ execFunction()
execFunctionNested()
execFunction()

// TODO: implement closures to access variables declared outside
//
//print('--------')
//
//fn secondExecFunction() {
// print('secondExecFunction')
//}
//
//fn secondExecFunctionNested() {
// print('secondExecFunctionNested')
// secondExecFunction()
//}
//
//secondExecFunction()
//secondExecFunctionNested()
//secondExecFunction()
print('--------')

fn secondExecFunction() {
print('secondExecFunction')
}

fn secondExecFunctionNested() {
print('secondExecFunctionNested')
secondExecFunction()
}

secondExecFunction()
secondExecFunctionNested()
secondExecFunction()

print('--------')

Expand Down
2 changes: 0 additions & 2 deletions hogvm/__tests__/loops.hog
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ print('-- test for loop --')
for (let i := 0; i < 3; i := i + 1) {
print(i) // prints 3 times
}
// TODO: test that this throws
// print(i) -- global does not print
}

print('-- test emptier for loop --')
Expand Down
48 changes: 23 additions & 25 deletions hogvm/__tests__/mandelbrot.hog
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,29 @@ fn mandelbrot(re, im, max_iter) {
}
}

// TODO: implement closures to access variables declared outside
fn main() {
let width := 80
let height := 24
let xmin := -2.0
let xmax := 1.0
let ymin := -1.0
let ymax := 1.0
let max_iter := 30

//fn main() {
let width := 80
let height := 24
let xmin := -2.0
let xmax := 1.0
let ymin := -1.0
let ymax := 1.0
let max_iter := 30

let y := 0
while(y < height) {
let row := ''
let x := 0
while (x < width) {
let re := x / width * (xmax - xmin) + xmin
let im := y / height * (ymax - ymin) + ymin
let letter := mandelbrot(re, im, max_iter)
row := concat(row, letter)
x := x + 1
let y := 0
while(y < height) {
let row := ''
let x := 0
while (x < width) {
let re := x / width * (xmax - xmin) + xmin
let im := y / height * (ymax - ymin) + ymin
let letter := mandelbrot(re, im, max_iter)
row := concat(row, letter)
x := x + 1
}
print(row)
y := y + 1
}
print(row)
y := y + 1
}
//}
//
//main()

main()
Loading

0 comments on commit c34ef5d

Please sign in to comment.