Skip to content

Small updates #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions antlr-parser/examples/all-features.lang
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,32 @@ func foo(a: Int, b: [Int]) {
}
}
func bar() -> Test {
var v = new Test { intArray = new [Tree] {} }
var v = new Test { intArray = new [Tree] { new Tree{left=null,right=null} } }
return v
}
func main() {
var m = 42
var j: Int
var t: Tree
var b = true
var c: Bool
var array = new [Int] {size=10,1,2,3}
array[1] = 42
t.left = null
if (m < 1)
print(1)
else if (m == 5)
print(2)
else
print(3)
}
func fib(n: Int)->Int {
var f1=1
var f2=1
var i=n
while( i>1 ){
var temp = f1+f2
f1=f2
f2=temp
i=i-1
}
return f2
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,17 @@ varDeclaration
;

typeName
: simpleType
: nominalType
| arrayType
;

simpleType
: IDENTIFIER ('?')?
nominalType
: 'Int'
| IDENTIFIER ('?')?
;

arrayType
: '[' simpleType ']' ('?')?
: '[' nominalType ']' ('?')?
;

functionDeclaration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,22 +173,19 @@ func foo()->Int {
public void testFunction9() {
String src = """
func fib(n: Int)->Int {
var i: Int;
var temp: Int;
var f1=1;
var f2=1;
i=n;
var f1=1
var f2=1
var i=n
while( i>1 ){
temp = f1+f2;
f1=f2;
f2=temp;
i=i-1;
var temp = f1+f2
f1=f2
f2=temp
i=i-1
}
return f2;
return f2
}

func foo()->Int {
return fib(10);
return fib(10)
}
""";
var value = compileAndRun(src, "foo", Options.OPT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,22 +334,17 @@ private AST.Expr parseNew(Lexer lexer) {
AST.TypeExpr resultType = parseTypeExpr(lexer);
var newExpr = new AST.NewExpr(resultType);
List<AST.Expr> initExpr = new ArrayList<>();
int initType = 0;
int index = 0;
if (testPunctuation(lexer, "{")) {
while (!isToken(currentToken, "}")) {
if (currentToken.kind == Token.Kind.IDENT && lexer.peekChar() == '=') {
if (initType == 0) initType = 1;
else if (initType != 1) throw new CompilerException("Cannot mix initializer expressions");
String fieldname = currentToken.str;
nextToken(lexer);
matchPunctuation(lexer, "=");
AST.Expr value = parseBool(lexer);
initExpr.add(new AST.InitFieldExpr(newExpr, fieldname, value));
}
else {
if (initType == 0) initType = 2;
else if (initType != 2) throw new CompilerException("Cannot mix initializer expressions");
var indexLit = Integer.valueOf(index++);
var indexExpr = new AST.LiteralExpr(Token.newNum(indexLit,indexLit.toString(),0));
initExpr.add(new AST.ArrayInitExpr(newExpr, indexExpr, parseBool(lexer)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func bar() -> Test {
func main() {
var m = 42
var t: Tree
var array = new [Int] {1,2,3}
var array = new [Int] {size=10,1,2,3}
array[1] = 42
t.left = null
if (m < 1)
Expand Down
20 changes: 9 additions & 11 deletions seaofnodes/src/test/cases/fib/fib.ez
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
func fib(n: Int)->Int {
var i: Int;
var temp=0;
var f1=1;
var f2=1;
i=n;
var f1=1
var f2=1
var i=n
while( i>1 ){
temp = f1+f2;
f1=f2;
f2=temp;
i=i-1;
var temp = f1+f2
f1=f2
f2=temp
i=i-1
}
return f2;
return f2
}

func foo()->Int {
return fib(10);
return fib(10)
}