Skip to content

Commit 2709306

Browse files
committed
added ECheckType, added short lambda parsing
1 parent 860a301 commit 2709306

File tree

11 files changed

+116
-3
lines changed

11 files changed

+116
-3
lines changed

.vscode/launch.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
// Utilisez IntelliSense pour en savoir plus sur les attributs possibles.
3+
// Pointez pour afficher la description des attributs existants.
4+
// Pour plus d'informations, visitez : https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "HashLink (launch)",
9+
"request": "launch",
10+
"type": "hl",
11+
"hxml": "hscript.hxml",
12+
"cwd": "${workspaceRoot}",
13+
"preLaunchTask": "Build"
14+
}
15+
]
16+
}

.vscode/tasks.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
// See https://go.microsoft.com/fwlink/?LinkId=733558
3+
// for the documentation about the tasks.json format
4+
"version": "2.0.0",
5+
"tasks": [
6+
{
7+
"label": "Build",
8+
"type": "hxml",
9+
"file": "hscript.hxml",
10+
"group": {
11+
"kind": "build",
12+
"isDefault": true
13+
}
14+
}
15+
]
16+
}

Test.hx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ class Test extends TestCase {
9999
assertScript("var a:Array<Dynamic>=[1,2,4]; a[2]", 4, null, true);
100100
assertScript("/**/0", 0);
101101
assertScript("x=1;x*=-2", -2);
102+
assertScript("var f = x -> x + 1; f(3)", 4);
103+
assertScript("var f = (x) -> x + 1; f(3)", 4);
104+
assertScript("var f = (x:Int) -> x + 1; f(3)", 4);
105+
assertScript("var f = (x,y) -> x + y; f(3,1)", 4);
106+
assertScript("var f = (x,y:Int) -> x + y; f(3,1)", 4);
107+
assertScript("var f = (x:Int,y:Int) -> x + y; f(3,1)", 4);
102108
}
103109

104110
function testMap():Void {

hscript.hxml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
bin/build-flash.hxml
1+
bin/build-each.hxml
2+
-hl bin/Test.hl

hscript/Bytes.hx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,8 @@ class Bytes {
246246
bout.addByte(args == null ? 0 : args.length + 1);
247247
if( args != null ) for( e in args ) doEncode(e);
248248
doEncode(e);
249+
case ECheckType(e,_):
250+
doEncode(e);
249251
}
250252
}
251253

@@ -374,6 +376,8 @@ class Bytes {
374376
var count = bin.get(pin++);
375377
var args = count == 0 ? null : [for( i in 0...count - 1 ) doDecode()];
376378
EMeta(name, args, doDecode());
379+
case 26:
380+
ECheckType(doDecode(), CTPath(["Void"]));
377381
case 255:
378382
null;
379383
default:

hscript/Expr.hx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ enum Expr {
6868
ESwitch( e : Expr, cases : Array<{ values : Array<Expr>, expr : Expr }>, ?defaultExpr : Expr);
6969
EDoWhile( cond : Expr, e : Expr);
7070
EMeta( name : String, args : Array<Expr>, e : Expr );
71+
ECheckType( e : Expr, t : CType );
7172
}
7273

7374
typedef Argument = { name : String, ?t : CType, ?opt : Bool, ?value : Expr };

hscript/Interp.hx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,8 @@ class Interp {
574574
return val;
575575
case EMeta(_, _, e):
576576
return expr(e);
577+
case ECheckType(e,_):
578+
return expr(e);
577579
}
578580
return null;
579581
}

hscript/Macro.hx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,8 @@ class Macro {
238238
case EMeta(m, params, esub):
239239
var mpos = #if hscriptPos { file : p.file, min : e.pmin, max : e.pmax } #else p #end;
240240
EMeta({ name : m, params : params == null ? [] : [for( p in params ) convert(p)], pos : mpos }, convert(esub));
241+
case ECheckType(e, t):
242+
ECheckType(convert(e), convertType(t));
241243
}, pos : #if hscriptPos { file : p.file, min : e.pmin, max : e.pmax } #else p #end }
242244
}
243245

hscript/Parser.hx

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,8 +359,31 @@ class Parser {
359359
return parseExprNext(mk(EConst(c)));
360360
case TPOpen:
361361
var e = parseExpr();
362-
ensure(TPClose);
363-
return parseExprNext(mk(EParent(e),p1,tokenMax));
362+
tk = token();
363+
switch( tk ) {
364+
case TPClose:
365+
return parseExprNext(mk(EParent(e),p1,tokenMax));
366+
case TDoubleDot:
367+
var t = parseType();
368+
tk = token();
369+
switch( tk ) {
370+
case TPClose:
371+
return parseExprNext(mk(ECheckType(e,t),p1,tokenMax));
372+
case TComma:
373+
switch( expr(e) ) {
374+
case EIdent(v): return parseLambda([{ name : v, t : t }], pmin(e));
375+
default:
376+
}
377+
default:
378+
}
379+
case TComma:
380+
switch( expr(e) ) {
381+
case EIdent(v): return parseLambda([{name:v}], pmin(e));
382+
default:
383+
}
384+
default:
385+
}
386+
return unexpected(tk);
364387
case TBrOpen:
365388
tk = token();
366389
switch( tk ) {
@@ -450,6 +473,25 @@ class Parser {
450473
}
451474
}
452475

476+
function parseLambda( args : Array<Argument>, pmin ) {
477+
while( true ) {
478+
var id = getIdent();
479+
var t = maybe(TDoubleDot) ? parseType() : null;
480+
args.push({ name : id, t : t });
481+
var tk = token();
482+
switch( tk ) {
483+
case TComma:
484+
case TPClose:
485+
break;
486+
default:
487+
unexpected(tk);
488+
}
489+
}
490+
ensureToken(TOp("->"));
491+
var eret = parseExpr();
492+
return mk(EFunction(args, mk(EReturn(eret),pmin)), pmin);
493+
}
494+
453495
function parseMetaArgs() {
454496
var tk = token();
455497
if( tk != TPOpen ) {
@@ -709,6 +751,21 @@ class Parser {
709751
var tk = token();
710752
switch( tk ) {
711753
case TOp(op):
754+
755+
if( op == "->" ) {
756+
// single arg reinterpretation of `f -> e` , `(f) -> e` and `(f:T) -> e`
757+
switch( expr(e1) ) {
758+
case EIdent(i), EParent(expr(_) => EIdent(i)):
759+
var eret = parseExpr();
760+
return mk(EFunction([{ name : i }], mk(EReturn(eret),pmin(eret))), pmin(e1));
761+
case ECheckType(expr(_) => EIdent(i), t):
762+
var eret = parseExpr();
763+
return mk(EFunction([{ name : i, t : t }], mk(EReturn(eret),pmin(eret))), pmin(e1));
764+
default:
765+
}
766+
unexpected(tk);
767+
}
768+
712769
if( unops.get(op) ) {
713770
if( isBlock(e1) || switch(expr(e1)) { case EParent(_): true; default: false; } ) {
714771
push(tk);

hscript/Printer.hx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,12 @@ class Printer {
300300
}
301301
add(" ");
302302
expr(e);
303+
case ECheckType(e, t):
304+
add("(");
305+
expr(e);
306+
add(" : ");
307+
addType(t);
308+
add(")");
303309
}
304310
}
305311

hscript/Tools.hx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class Tools {
5656
}
5757
if( def != null ) f(def);
5858
case EMeta(name, args, e): if( args != null ) for( a in args ) f(a); f(e);
59+
case ECheckType(e,_): f(e);
5960
}
6061
}
6162

@@ -88,6 +89,7 @@ class Tools {
8889
case ETernary(c, e1, e2): ETernary(f(c), f(e1), f(e2));
8990
case ESwitch(e, cases, def): ESwitch(f(e), [for( c in cases ) { values : [for( v in c.values ) f(v)], expr : f(c.expr) } ], def == null ? null : f(def));
9091
case EMeta(name, args, e): EMeta(name, args == null ? null : [for( a in args ) f(a)], f(e));
92+
case ECheckType(e,t): ECheckType(f(e), t);
9193
}
9294
#if hscriptPos
9395
return { e : edef, pmin : e.pmin, pmax : e.pmax, origin : e.origin, line : e.line };

0 commit comments

Comments
 (0)