[fine] More resilience

We don't lose function declarations and whatnot, although we get lost
with broken return types.
This commit is contained in:
John Doty 2024-01-31 07:46:20 -08:00
parent 93c9dcd13b
commit 7f30d0ccc3
3 changed files with 98 additions and 39 deletions

View file

@ -8,10 +8,9 @@ class Bar {
}
fun extract_value(v: Foo or Bar) -> f64 {
if v is Foo {
v.x // Magic type binding!
} else if v is Bar {
v.y // Same magic re-binding of the variable to the new type
match v {
v:Foo -> v.x,
v:Bar -> v.y,
} // No error; exhaustivity analysis should work.
}
@ -34,10 +33,9 @@ class Monster {
fun print(x:string) {}
fun in_range(weapon: MeleeWeapon or RangedWeapon, distance: f64) {
if weapon is w : RangedWeapon {
distance >= w.minRange and distance <= w.maxRange
} else {
distance == 1
weapon match {
w:RangedWeapon -> distance >= w.minRange and distance <= w.maxRange,
_ -> distance == 1
}
}
@ -60,12 +58,9 @@ fun attack(weapon: MeleeWeapon or RangedWeapon, monster: Monster, distance: f64)
// local variables. The *almost* part is that the effective type of the
// variable changes but not the binding. (Is this what we want?)
//
// TODO: What do we do about captured variables?
//
let damage = if weapon is w: MeleeWeapon {
roll_dice(w.damage)
} else if weapon is w: RangedWeapon {
w.maxRange - w.minRange
let damage = match weapon {
w:MeleeWeapon -> roll_dice(w.damage),
w:RangedWeapon -> w.maxRange - w.minRange,
};
if monster.health <= damage {
@ -87,7 +82,7 @@ fun more_examples(weapon: MeleeWeapon or RangedWeapon) -> f64 or () {
// Some fun with iterators
class Finished {}
let FINISHED = new Finished {}
let FINISHED = new Finished {};
class Iterator {
current: f64;
@ -132,5 +127,5 @@ fun test() -> f64 {
// like the above.
}
// @ignore WIP
/// @ignore WIP
// @no-errors

View file

@ -17,6 +17,5 @@ fun test() {
// matklad talks about in his resilient parser article.
//
// @expect-errors:
// | 7:12: Error at '{': expected an expression
// | 7:12: Error at '{': expect ';' to end a let statement
// | 7:26: cannot find value foo here
// | 8:0: Error at '}': unbalanced '}'