Added import support

This commit is contained in:
YoshiCrafter29
2022-06-08 20:10:40 +02:00
parent 7124b6c4ce
commit 841261f199
5 changed files with 774 additions and 673 deletions
+2
View File
@@ -70,6 +70,7 @@ enum Expr {
EDoWhile( cond : Expr, e : Expr);
EMeta( name : String, args : Array<Expr>, e : Expr );
ECheckType( e : Expr, t : CType );
EImport( c : String );
}
typedef Argument = { name : String, ?t : CType, ?opt : Bool, ?value : Expr };
@@ -117,6 +118,7 @@ enum Error {
EInvalidOp( op : String );
EInvalidAccess( f : String );
ECustom( msg : String );
EInvalidClass( className : String);
}
+710 -670
View File
File diff suppressed because it is too large Load Diff
+55 -2
View File
@@ -347,13 +347,15 @@ class Parser {
}
function parseExpr() {
var oldPos = readPos;
var tk = token();
#if hscriptPos
var p1 = tokenMin;
#end
switch( tk ) {
case TId(id):
var e = parseStructure(id);
var e = parseStructure(id, oldPos);
if( e == null )
e = mk(EIdent(id));
return parseExprNext(e);
@@ -579,7 +581,7 @@ class Parser {
}
}
function parseStructure(id) {
function parseStructure(id, ?oldPos:Int) {
#if hscriptPos
var p1 = tokenMin;
#end
@@ -654,6 +656,57 @@ class Parser {
}
var inf = parseFunctionDecl();
mk(EFunction(inf.args, inf.body, name, inf.ret),p1,pmax(inf.body));
case "import":
var oldReadPos = readPos;
var tk = token();
switch( tk ) {
case TPOpen:
trace("using base thing shit");
var tok = token();
switch(tok) {
case TConst(c):
switch(c) {
case CString(s):
token();
ensure(TSemicolon);
push(TSemicolon);
mk(EImport(s), p1);
default:
unexpected(tok);
null;
}
default:
unexpected(tok);
null;
}
case TId(id):
var path = [id];
var t = null;
while( true ) {
t = token();
if( t != TDot ) {
push(t);
break;
}
t = token();
switch( t ) {
case TId(id):
path.push(id);
// case TOp("*"):
// star = true;
default:
unexpected(t);
}
}
ensure(TSemicolon);
push(TSemicolon);
var p = path.join(".");
mk(EImport(p),p1);
default:
unexpected(tk);
null;
}
case "return":
var tk = token();
push(tk);
+4 -1
View File
@@ -114,6 +114,8 @@ class Printer {
return;
}
switch( #if hscriptPos e.e #else e #end ) {
case EImport(c):
add("import " + c);
case EConst(c):
switch( c ) {
case CInt(i): add(i);
@@ -331,7 +333,7 @@ class Printer {
public static function errorToString( e : Expr.Error ) {
var message = switch( #if hscriptPos e.e #else e #end ) {
case EInvalidChar(c): "Invalid character: '"+(StringTools.isEof(c) ? "EOF" : String.fromCharCode(c))+"' ("+c+")";
case EInvalidChar(c): "Invalid character: '"+(StringTools.isEof(c) ? "EOF (End Of File)" : String.fromCharCode(c))+"' ("+c+")";
case EUnexpected(s): "Unexpected token: \""+s+"\"";
case EUnterminatedString: "Unterminated string";
case EUnterminatedComment: "Unterminated comment";
@@ -341,6 +343,7 @@ class Printer {
case EInvalidOp(op): "Invalid operator: "+op;
case EInvalidAccess(f): "Invalid access to field " + f;
case ECustom(msg): msg;
case EInvalidClass(cla): "Invalid class: " + cla + " was not found.";
};
#if hscriptPos
return e.origin + ":" + e.line + ": " + message;
+3
View File
@@ -27,6 +27,7 @@ class Tools {
public static function iter( e : Expr, f : Expr -> Void ) {
switch( expr(e) ) {
case EConst(_), EIdent(_):
case EImport(c): f(e);
case EVar(_, _, e): if( e != null ) f(e);
case EParent(e): f(e);
case EBlock(el): for( e in el ) f(e);
@@ -57,6 +58,7 @@ class Tools {
if( def != null ) f(def);
case EMeta(name, args, e): if( args != null ) for( a in args ) f(a); f(e);
case ECheckType(e,_): f(e);
}
}
@@ -86,6 +88,7 @@ class Tools {
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));
case EMeta(name, args, e): EMeta(name, args == null ? null : [for( a in args ) f(a)], f(e));
case ECheckType(e,t): ECheckType(f(e), t);
case EImport(c): EImport(c);
}
return mk(edef, e);
}