Skip to content

Latest commit

History

132 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

LanguageCreator

A place to practice language creation mechanisms. Currently can execute a weird minimal language inspired by c# and f#. It properly contructs an AST, validates scoping rules, infer types, and properly uses encapsulated memory spaces.

Supported Constructs

The language supports:

  • variable assignment
  • if/else conditionals declaration
  • function declaration with variable arguments
  • anonymous functions
  • quoted strings
  • simple type inference
  • basic classes (no inheritance)
  • exception handling
  • nil support
  • partial function creation and application

Supported built in types are:

  • int
  • float
  • string
  • void
  • bool (Booleans are true and false)

Operations are:

  • +
  • -
  • & (and)
  • || (or)
  • /
  • ^

Regarding type inference:

  • Anonymous lambdas can take arguments and return values when stored in a type inferred "var" variable
  • Regular functions can also be declared as "var" and type inferred from their return types
  • You can enforce static typing rules by giving a function a proper type, otherwise it'll just use the return type
  • If no return statement exists it'll default to void.

Closures can reference their parent's memory space but not their callers memory space.

If a class has a function called init then that is treated as the constructor and is called when a class is created.

Class instantiation is scala style, it is just top down. If a statement isn't enclosed in a function it will be executed before calling the init function.

Example1

voidfoo(intx){if(x>2){print((x+1)+2);}else{print(x);}}foo(1);foo(100);
1
103

Example2

varx=fun(intarg) -> {int g =arg;while(g>0){printg;g=g-1;}
print "done!";}vary=x;varz=y;z(5);
print "lambda assigments work!";z(3);inta=1;intb=a;intc=b;printc;
5
4
3
2
1
done!
lambda assigments work!
3
2
1
done!
1

Example3

varfoo(stringt){varx="test";returnx+t;}printfoo("pong");
testpong

Example4 (currying)

varfunc(stringprinter,intx){printprinter;printx;}varcurry=func("anton");curry(1);curry(2);varotherCurry=func("test");otherCurry(3);
anton
1
anton
2
test
3

Example 5 (classes)

classanton{intx=1;inty=2;voidfoo(){printx;}}varant=newanton();varfoo=newanton();foo.x=2;ant.foo();foo.foo();foo.x=10;foo.foo();
1
2
10

Example 6

classbob{varz=1;}classanton{varx=newbob();inty=0;}antonfoo=newanton();printfoo.x.z;
1

Example 7

classbob{varz=1;}classanton{varx=newbob();inty=0;}antonfoo=newanton();printfoo.x.z;foo.x.z=2;printfoo.x.z;
1
2

Example 8

classanton{varx=fun() -> {returnnewanton();};
inty=10;}varx=newanton();vardynamicAnton=x.x();dynamicAnton.y=52;printdynamicAnton.y;
52

Example 9 (forward referencing and object passing)

classhuman{voidinit(stringid){age=99;name=id;}voidcreate(){person=newhuman('test');}intage;stringname;humanperson;}varperson=newhuman('anton');voidprintPerson(humanperson){print 'age of ' +person.name+' = ';printperson.age;print '----';}person.age=29;person.create();printPerson(person);printPerson(person.person);
age of anton = 29
----
age of jane doe = 99
----

Example 10 basic closures

classbob{intx=0;stringpr1(methodx){returnx('test')+' in class bob pr1';}}classhuman{intx=1;varb=newbob();voidpr(methodz){printb.pr1(z)+' from class human pr';}}vara=newhuman();varb=newbob();intx=100;varlambda=fun(stringv) ->{var p =fun() -> {x=x+1;printx;printv+' in second lambda';};p();returnv;};a.pr(lambda);printb.pr1(lambda)+' from main';printx;
101
test in second lambda
test in class bob pr1 from class human pr
102
test in second lambda
test in class bob pr1 from main
102

Example 11 (basic reference linking)

intx=1;inty=&x;printy;y=2;printx;y=3;printx;x=4;printy;
1
2
3
4

Example 12 (complex reference linking passing via different memory scopes)

classbob{intx=0;stringpr1(methodx){returnx('test')+' in class bob pr1';}}classhuman{intx=1;varb=newbob();voidpr(methodz){printb.pr1(z)+' from class human pr';}}vara=newhuman();varb=newbob();inty=100;intf=&y;intx=&f;varlambda=fun(stringv) ->{var p =fun() -> {x=x+1;printx;printv+' in second lambda';};p();returnv;};a.pr(lambda);printb.pr1(lambda)+' from main';printy;
101
test in second lambda
test in class bob pr1 from class human pr
102
test in second lambda
test in class bob pr1 from main
102

(note, this is supposed to have the same result as example 10 which has no links)

Example 13 - nil comparison

voidprintNull(intitem){if(item==nil){print 'is nil';
}else{print 'is not nil';
}}intx;inty=1;printNull(x);printNull(y);x=2;printNull(x);
is nil
is not nil
is not nil

Example 14 - simple exception handling

classtest{intx;}testitem;try{printitem.x;}catch{print 'exception!';
}
exception!

Notes

Type promotion doesn't exist and neither does inheritance. So you can't print a string and an int on the same line because the expression won't match properly, but thats intentional right now.

About

A homebrew toy programming language. Constructs an AST and validates scoping rules. Has type inference, supports functions and currying, classes with constructors (but no inheritance), while and for loops, if/elseif/else conditionals, exceptions, dynamic objects, and more

Resources

Stars

42 stars

Watchers

4 watching

Forks

Releases

Packages

Used by

Contributors

Languages