Om scripting progress

Published March 15, 2015
Advertisement
Flying along with the scripting language. Have implemented Array types as well as Object types now. Example of some of the things you can do:[code=:0]var o ={ var x = [ 10, 20, "hello" ]; var f = func { return this; };};var a ={ var x = 20;};a.f = o.f;out ++o.f().x[1], "\n";out o.x[1], "\n";out a.f().x, "\n";for(var i = 0; i < 3; ++i) out o.x, "\n";var i = 20;while(i >= 2){ if(i < 10) out "0"; out i--, "\n";}var f = func(a, b){ return a + b; }out f(10, 20), "\n";
I particularly like this line:

out ++o.f().x[1];

This applies the preincrement to the first element of the x array, which is a property of the object returned by f(), which is returning "this" so, because it is called in the context of o, returns o, meaning that x is then the array member of o.

All this is made possible by a two pass system, the first pass converting the source into a tree of nodes, then the second pass calling a virtual generate() method on the root node, that recursively generates bytecode for the child nodes. The rule for most nodes is that they leave their value on the top of the runtime stack.

So the program above generates the following node tree:[code=:0]function block var o object member x array element int 10 element int 20 element string "hello" member f function block return this var a object member x int 20 expression assign dot symbol a f dot symbol o f out preincdec 1 subscript dot call dot symbol o f x int 1 out string "\n" out subscript dot symbol o x int 1 out string "\n" out dot call dot symbol a f x out string "\n" for varexpr i int 0 binary lt symbol i int 3 preincdec 1 symbol i block out subscript dot symbol o x symbol i out string "\n" var i int 20 while binary gteq symbol i int 2 block if binary lt symbol i int 10 block out string "0" out postincdec -1 symbol i out string "\n" var f function param a param b block return binary add symbol a symbol b out call symbol f param int 10 param int 20 out string "\n"

Which generates the following bytecode:[code=:0]Heap 0 refs: 0 1 refs: 0function 0: MkObj 1: MkArr 2: Push int 10 8: AddAr 9: Push int 20 15: AddAr 16: MkStr 4 21: AddAr 22: AddMb 13 27: Push function 2 33: AddMb 18 38: SetId 2 43: MkObj 44: Push int 20 50: AddMb 13 55: SetId 3 60: Peek 1 65: GetMb 18 70: Peek 1 75: SetMb 18 80: Pop 81: Push null null 87: Peek 2 92: Peek 3 97: GetMb 18102: Push null null108: Call109: GetMb 13114: Push int 1120: SubGet121: Push int 1127: Add128: Push null null134: Peek 3139: Peek 4144: GetMb 18149: Push null null155: Call156: GetMb 13161: Push int 1167: SubPut168: Out169: MkStr 23174: Out175: Peek 1180: GetMb 13185: Push int 1191: SubGet192: Out193: MkStr 23198: Out199: Push null null205: Peek 1210: Peek 2215: GetMb 18220: Push null null226: Call227: GetMb 13232: Out233: MkStr 23238: Out239: Push int 0245: SetId 4250: Peek 0255: Pop256: Peek 0261: Push int 3267: Lt268: JmpZ 319273: Peek 2278: GetMb 13283: Peek 1288: SubGet289: Out290: MkStr 23295: Out296: Peek 0301: Push int 1307: Add308: Poke 1313: Pop314: Jmp 256319: Pop320: Push int 20326: SetId 5331: Peek 0336: Push int 2342: GtEq343: JmpZ 406348: Peek 0353: Push int 10359: Lt360: JmpZ 371365: MkStr 28370: Out371: Peek 0376: Peek 1381: Push int -1387: Add388: Poke 2393: Pop394: Out395: MkStr 23400: Out401: Jmp 331406: Push function 3412: SetId 9417: Push null null423: Push null null429: Push int 10435: Push int 20441: Peek 4446: Push null null452: Call453: Out454: MkStr 23459: Out460: Pop461: Pop462: Pop463: Pop464: Pop465: Ret 2 refs: 1function 0: Peek 0 5: Poke 210: Pop11: Pop12: Ret 3 refs: 1function 0: Peek 1 5: Peek 110: Add11: Poke 416: Pop17: Pop18: Pop19: Pop20: Ret
The runtime then executes this bytecode, using reference counting to know when to destroy entities such as strings, objects and arrays.

All good fun.
Previous Entry Side Project - Om Script
Next Entry Om
6 likes 3 comments

Comments

Migi0027

This is seriously awesome! Makes my scripting language look horrible :P

March 15, 2015 11:55 AM
evolutional
Nice work. I have a particular fondness for scripting languages and their implementation. All this is making me want to look at my old projects again!
March 15, 2015 01:11 PM
Aardvajk

Thanks guys. I spent a lot of years working on this stuff a while back and has been fun to jump back into again.

March 16, 2015 10:24 AM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement
Advertisement