I’ve managed to put together a scripting language compiler that has the following features:
- Native data types: bool, string, char, int + float (I need to add vector types)
- C-like syntax
- Functions
- Automatic POD casting. I.e., every data type can be casted to another. This will be true even when I add classes or structs, not sure how…
- Ability to call “Engine” functions. I.e., you seed the Compiler with possible function calls that can be made of the platform
The compiler converts something like:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
int AddAndPow(int a, int b); int main() { int i = engine.RandomNum(25235,1,"234093"); int j = engine.RandomNum(598498,1,10); i = AddAndPow(i,j); } int AddAndPow(int a, int b) { return (a + b); } |
Into:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
DATA 0 "234093" EDAT STRT 3 PUSH $0 CAST int PUSH #1 PUSH #25235 ENGE RandomNum STOR 4 PUSH #10 PUSH #1 PUSH #598498 ENGE RandomNum STOR 5 PUSH 5 PUSH 4 JUMP 0 STOR 4 ENDL 3 STRT 0 STOR 1 STOR 2 PUSH 1 PUSH 2 ADD RTRN ENDL 0 |
Which should be very simple for a Virtual Machine to run through and execute. You can see that no optimization has taken place; I’ll probably think about doing that after getting it to actually run.