implicit return in blocks
This commit is contained in:
parent
431e8f9a66
commit
3a9e23b82e
|
@ -33,6 +33,14 @@ proc beginScope*(comp: Compiler, function: bool = false) =
|
||||||
for label in scope.labels:
|
for label in scope.labels:
|
||||||
comp.addLocal(&":{label}", delta = 0)
|
comp.addLocal(&":{label}", delta = 0)
|
||||||
|
|
||||||
|
proc scopeRetIndex*(comp: Compiler): int =
|
||||||
|
let scope = comp.scopes[comp.scopes.high()]
|
||||||
|
when assertionsCompiler:
|
||||||
|
# this is an illegal operation for function scopes, as they work differently
|
||||||
|
if scope.function:
|
||||||
|
comp.error("Assertion failed, Internal error, scopeRetIndex calculation for a function scope.")
|
||||||
|
return scope.goalStackIndex
|
||||||
|
|
||||||
proc restore*(comp: Compiler, scope: Scope) =
|
proc restore*(comp: Compiler, scope: Scope) =
|
||||||
let delta = comp.stackIndex - scope.goalStackIndex
|
let delta = comp.stackIndex - scope.goalStackIndex
|
||||||
comp.writePops(delta)
|
comp.writePops(delta)
|
||||||
|
|
|
@ -51,8 +51,13 @@ proc statement*(comp: Compiler) =
|
||||||
comp.breakStatement()
|
comp.breakStatement()
|
||||||
else:
|
else:
|
||||||
comp.expression()
|
comp.expression()
|
||||||
|
if comp.current.tokenType == tkRightBrace:
|
||||||
|
# last expression in the block expression -> semicolon optional
|
||||||
|
comp.writeChunk(0, opSetLocal)
|
||||||
|
comp.writeChunk(0, comp.scopeRetIndex().toDU8())
|
||||||
|
else:
|
||||||
|
comp.consume(tkSemicolon, "Semicolon expected after expression statement.")
|
||||||
comp.writeChunk(-1, opPop)
|
comp.writeChunk(-1, opPop)
|
||||||
comp.consume(tkSemicolon, "Semicolon expected after expression statement.")
|
|
||||||
|
|
||||||
if comp.panicMode:
|
if comp.panicMode:
|
||||||
comp.synchronize()
|
comp.synchronize()
|
|
@ -1,27 +1,27 @@
|
||||||
// cascade
|
// cascade
|
||||||
|
|
||||||
var f1 = funct() { @result
|
var f1 = funct() {
|
||||||
var x = 1;
|
var x = 1;
|
||||||
var y = 5;
|
var y = 5;
|
||||||
:result = funct() { @result
|
funct() {
|
||||||
var z = 8;
|
var z = 8;
|
||||||
print (x);
|
print (x);
|
||||||
x = x + 1;
|
x = x + 1;
|
||||||
:result = funct() { @result
|
funct() {
|
||||||
print (x);
|
print (x);
|
||||||
x = x + 1;
|
x = x + 1;
|
||||||
:result = funct() { @result
|
funct() {
|
||||||
print (x);
|
print (x);
|
||||||
print (y);
|
print (y);
|
||||||
print (z);
|
print (z);
|
||||||
x = x + 1;
|
x = x + 1;
|
||||||
:result = funct() {
|
funct() {
|
||||||
print (x);
|
print (x);
|
||||||
x = x + 1;
|
x = x + 1;
|
||||||
};
|
}
|
||||||
};
|
}
|
||||||
};
|
}
|
||||||
};
|
}
|
||||||
};
|
};
|
||||||
f1()()()()();
|
f1()()()()();
|
||||||
//expect:1.0
|
//expect:1.0
|
||||||
|
@ -33,13 +33,13 @@ f1()()()()();
|
||||||
|
|
||||||
// capturing closures in lists:
|
// capturing closures in lists:
|
||||||
|
|
||||||
var f = funct() { @result
|
var f = funct() {
|
||||||
var y = 5;
|
var y = 5;
|
||||||
var x = @[
|
var x = @[
|
||||||
funct() print (y),
|
funct() print (y),
|
||||||
funct() y = y + 1
|
funct() y = y + 1
|
||||||
];
|
];
|
||||||
:result = x;
|
x
|
||||||
};
|
};
|
||||||
|
|
||||||
var inst = f();
|
var inst = f();
|
||||||
|
|
Loading…
Reference in New Issue