Parser Seems done!

This commit is contained in:
Mike Nolan 2023-07-26 22:47:21 -05:00
parent 94f6de20ce
commit c48bd44bc3
2 changed files with 105 additions and 14 deletions

View File

@ -77,70 +77,70 @@ namespace TLang.Parser
var symbolNode = expr as SymbolNode; var symbolNode = expr as SymbolNode;
if(symbolNode == null) throw new CompilerError("Assigning variable to something that is not a symbol",expr); if(symbolNode == null) throw new CompilerError("Assigning variable to something that is not a symbol",expr);
expr = new SetVariableNode(symbolNode.Name,ParseLOr()){LineInfo=token.Position}; expr = new SetVariableNode(symbolNode,ParseLOr()){LineInfo=token.Position};
} }
if(token.Text == "+=") if(token.Text == "+=")
{ {
var symbolNode = expr as SymbolNode; var symbolNode = expr as SymbolNode;
if(symbolNode == null) throw new CompilerError("Assigning variable to something that is not a symbol",expr); if(symbolNode == null) throw new CompilerError("Assigning variable to something that is not a symbol",expr);
expr = new SetVariableNode(symbolNode.Name,new AddNode(new GetVariableNode(symbolNode.Name),ParseLOr())){LineInfo=token.Position}; expr = new SetVariableNode(symbolNode,new AddNode(new GetVariableNode(symbolNode.Name),ParseLOr())){LineInfo=token.Position};
} }
if(token.Text == "-=") if(token.Text == "-=")
{ {
var symbolNode = expr as SymbolNode; var symbolNode = expr as SymbolNode;
if(symbolNode == null) throw new CompilerError("Assigning variable to something that is not a symbol",expr); if(symbolNode == null) throw new CompilerError("Assigning variable to something that is not a symbol",expr);
expr = new SetVariableNode(symbolNode.Name,new SubtractNode(new GetVariableNode(symbolNode.Name),ParseLOr())){LineInfo=token.Position}; expr = new SetVariableNode(symbolNode,new SubtractNode(new GetVariableNode(symbolNode.Name),ParseLOr())){LineInfo=token.Position};
} }
if(token.Text == "*=") if(token.Text == "*=")
{ {
var symbolNode = expr as SymbolNode; var symbolNode = expr as SymbolNode;
if(symbolNode == null) throw new CompilerError("Assigning variable to something that is not a symbol",expr); if(symbolNode == null) throw new CompilerError("Assigning variable to something that is not a symbol",expr);
expr = new SetVariableNode(symbolNode.Name,new MultiplyNode(new GetVariableNode(symbolNode.Name),ParseLOr())){LineInfo=token.Position}; expr = new SetVariableNode(symbolNode,new MultiplyNode(new GetVariableNode(symbolNode.Name),ParseLOr())){LineInfo=token.Position};
} }
if(token.Text == "/=") if(token.Text == "/=")
{ {
var symbolNode = expr as SymbolNode; var symbolNode = expr as SymbolNode;
if(symbolNode == null) throw new CompilerError("Assigning variable to something that is not a symbol",expr); if(symbolNode == null) throw new CompilerError("Assigning variable to something that is not a symbol",expr);
expr = new SetVariableNode(symbolNode.Name,new DivideNode(new GetVariableNode(symbolNode.Name),ParseLOr())){LineInfo=token.Position}; expr = new SetVariableNode(symbolNode,new DivideNode(new GetVariableNode(symbolNode.Name),ParseLOr())){LineInfo=token.Position};
} }
if(token.Text == "%=") if(token.Text == "%=")
{ {
var symbolNode = expr as SymbolNode; var symbolNode = expr as SymbolNode;
if(symbolNode == null) throw new CompilerError("Assigning variable to something that is not a symbol",expr); if(symbolNode == null) throw new CompilerError("Assigning variable to something that is not a symbol",expr);
expr = new SetVariableNode(symbolNode.Name,new ModuloNode(new GetVariableNode(symbolNode.Name),ParseLOr())){LineInfo=token.Position}; expr = new SetVariableNode(symbolNode,new ModuloNode(new GetVariableNode(symbolNode.Name),ParseLOr())){LineInfo=token.Position};
} }
if(token.Text == "|=") if(token.Text == "|=")
{ {
var symbolNode = expr as SymbolNode; var symbolNode = expr as SymbolNode;
if(symbolNode == null) throw new CompilerError("Assigning variable to something that is not a symbol",expr); if(symbolNode == null) throw new CompilerError("Assigning variable to something that is not a symbol",expr);
expr = new SetVariableNode(symbolNode.Name,new BOrNode(new GetVariableNode(symbolNode.Name),ParseLOr())){LineInfo=token.Position}; expr = new SetVariableNode(symbolNode,new BOrNode(new GetVariableNode(symbolNode.Name),ParseLOr())){LineInfo=token.Position};
} }
if(token.Text == "&=") if(token.Text == "&=")
{ {
var symbolNode = expr as SymbolNode; var symbolNode = expr as SymbolNode;
if(symbolNode == null) throw new CompilerError("Assigning variable to something that is not a symbol",expr); if(symbolNode == null) throw new CompilerError("Assigning variable to something that is not a symbol",expr);
expr = new SetVariableNode(symbolNode.Name,new BAndNode(new GetVariableNode(symbolNode.Name),ParseLOr())){LineInfo=token.Position}; expr = new SetVariableNode(symbolNode,new BAndNode(new GetVariableNode(symbolNode.Name),ParseLOr())){LineInfo=token.Position};
} }
if(token.Text == "^=") if(token.Text == "^=")
{ {
var symbolNode = expr as SymbolNode; var symbolNode = expr as SymbolNode;
if(symbolNode == null) throw new CompilerError("Assigning variable to something that is not a symbol",expr); if(symbolNode == null) throw new CompilerError("Assigning variable to something that is not a symbol",expr);
expr = new SetVariableNode(symbolNode.Name,new XOrNode(new GetVariableNode(symbolNode.Name),ParseLOr())){LineInfo=token.Position}; expr = new SetVariableNode(symbolNode,new XOrNode(new GetVariableNode(symbolNode.Name),ParseLOr())){LineInfo=token.Position};
} }
if(token.Text == "^^=") if(token.Text == "^^=")
{ {
var symbolNode = expr as SymbolNode; var symbolNode = expr as SymbolNode;
if(symbolNode == null) throw new CompilerError("Assigning variable to something that is not a symbol",expr); if(symbolNode == null) throw new CompilerError("Assigning variable to something that is not a symbol",expr);
expr = new SetVariableNode(symbolNode.Name,new PowNode(new GetVariableNode(symbolNode.Name),ParseLOr())){LineInfo=token.Position}; expr = new SetVariableNode(symbolNode,new PowNode(new GetVariableNode(symbolNode.Name),ParseLOr())){LineInfo=token.Position};
} }
} }
return expr; return expr;
@ -638,12 +638,102 @@ namespace TLang.Parser
} }
} }
else{ else{
Node ret = new Node(); SymbolNode ret = new NullNode();
if(token.Text == "func") if(token.Text == "func")
{ {
var res = new GetVariableNode(Context.NextEntry.Text); var res = new GetVariableNode(Context.NextEntry.Text);
if(Context.MoreTokens && Context.) if(Context.MoreTokens && Context.NextEntries(true,"(").Success)
{
List<string> args=new List<string>();
while(!Context.NextEntries(true,")").Success)
{
if(Context.NextEntries(true,",").Success) continue;
if(Context.Offset < Context.Tokens.Count)
{
var n0 = ParseNode();
var n= n0 as GetVariableNode;
if(n != null)
{
args.Add(n.Name);
}
else
{
throw new CompilerError("Argument must be a GetVariableNode",n0);
}
}
}
return new SetVariableNode( res,new ClosureNode(args,ParseNode()));
}
} }
if(token.Text == "null")
{
ret=new NullNode(){LineInfo=token.Position};
}
else if(token.Text == "undefined")
{
ret = new UndefinedNode(){LineInfo=token.Position};
}
else if(token.Text == "true")
{
ret = new TrueNode(){LineInfo=token.Position};
}
else if(token.Text == "false")
{
ret = new FalseNode(){LineInfo=token.Position};
}
else
{
ret = new GetVariableNode(token.Text){LineInfo = token.Position};
}
while(Context.MoreTokens && Context.NextEntries(true,"[").Success)
{
var p = ParseNode();
ret = new GetArrayNode(ret,p){LineInfo = p.LineInfo};
Context.NextEntries(true,"]");
}
while(Context.MoreTokens && Context.NextEntries(true,".").Success)
{
var token2 = Context.NextEntry;
if(Context.MoreTokens && Context.NextEntries(true,"(").Success)
{
var mCall = new MethodCallNode(ret,token2.Text){LineInfo=token2.Position};
ret = mCall;
while(Context.MoreTokens && !Context.NextEntries(true,")").Success)
{
if(Context.NextEntries(true,",").Success) continue;
mCall.Arguments.Add(ParseNode());
}
}
else if(Context.MoreTokens && Context.NextEntries(false,"[").Success)
{
while(Context.MoreTokens && Context.NextEntries(true,"[").Success)
{
var p = ParseNode();
ret = new GetArrayNode(ret,p){LineInfo = p.LineInfo};
Context.NextEntries(true,"]");
}
}
else
{
ret = new GetMemberNode(ret,token2.Text){LineInfo = token2.Position};
}
}
if(Context.MoreTokens && Context.NextEntries(true,"++").Success)
{
var r = new PostFixIncrementNode(ret){LineInfo = ret.LineInfo};
ret = r;
}
if(Context.MoreTokens && Context.NextEntries(true,"--").Success)
{
var r = new PostFixDecrementNode(ret){LineInfo = ret.LineInfo};
ret = r;
}
return ret;
} }
} }

View File

@ -3,11 +3,12 @@ namespace TLang.Parser
public class SetVariableNode : SymbolNode public class SetVariableNode : SymbolNode
{ {
public SymbolNode SetTo {get;set;}
public Node Expression {get;set;} public Node Expression {get;set;}
public SetVariableNode(string name, Node expr) public SetVariableNode(SymbolNode setTo, Node expr)
{ {
Name = name; SetTo = setTo;
Expression = expr; Expression = expr;
} }
} }