Permalink
Browse files

Finished implementation of calling funcs with args.

  • Loading branch information...
1 parent d00a5e6 commit 5239aebb2687e5e833154087085843f4f319a5b4 @smack0007 committed Apr 25, 2012
@@ -1,6 +1,6 @@
Script : { Func } EOF
-Func : [func] Identifier [(] { Variable [,]! } [)] [{] { Statement } [}]
+Func : [func] Identifier [(] { Expression [,]! } [)] [{] { Statement } [}]
Identifier : [A-Za-z_][A-Za-z0-9_]*
View
@@ -67,16 +67,17 @@ public void DisplayLexemes()
for (int i = 0; i < lexemes.Count; i++)
OutputLine(i + " => " + lexemes[i]);
}
-
- private void ThrowSyntaxException(string expected, int pos)
+
+ private void EnsureLexemeType(LexemeType expected, int pos)
{
- throw new ScriptException(ScriptError.SyntaxError, expected + " was expected at Line " + this.lexemes[pos].Line + " Column " + this.lexemes[pos].Column + ".");
+ if (this.lexemes[pos].Type != expected)
+ throw new ScriptException(ScriptError.SyntaxError, expected + " was expected at Line " + this.lexemes[pos].Line + " Column " + this.lexemes[pos].Column + ".");
}
- private void EnsureLexemeType(LexemeType expected, int pos)
+ private void EnsureNotLexemeType(LexemeType unexpected, int pos)
{
- if (this.lexemes[pos].Type != expected)
- this.ThrowSyntaxException(expected.ToString(), pos);
+ if (this.lexemes[pos].Type == unexpected)
+ throw new ScriptException(ScriptError.SyntaxError, unexpected + " was not expected at Line " + this.lexemes[pos].Line + " Column " + this.lexemes[pos].Column + ".");
}
/// <summary>
@@ -178,7 +179,7 @@ public Variable Execute(string funcName)
public Variable Execute(string funcName, IList<Variable> args)
{
if (!this.funcs.ContainsKey(funcName))
- throw new ScriptException(ScriptError.FunctionDoesNotExist, "No function found by the name " + funcName + ".");
+ throw new ScriptException(ScriptError.InvalidFunctionCall, "No function found by the name " + funcName + ".");
FuncInfo funcInfo = this.funcs[funcName];
int pos = funcInfo.EntryLocation;
@@ -187,10 +188,13 @@ public Variable Execute(string funcName, IList<Variable> args)
if (args != null)
{
+ if (args.Count != funcInfo.Args.Length)
+ throw new ScriptException(ScriptError.InvalidFunctionCall, "Invalid number of args specified for " + funcName + ".");
+
for (int i = 0; i < args.Count; i++)
{
Variable arg = this.variableStack[funcInfo.Args[i]];
- arg.Val = args[i].Val;
+ arg.Gets(args[i]);
}
}
@@ -478,21 +482,24 @@ private Variable FuncCall(ref int pos)
pos++;
List<Variable> args = new List<Variable>();
- while (this.lexemes[pos].Type == LexemeType.Variable)
+ while (this.lexemes[pos].Type != LexemeType.CloseParen &&
+ this.lexemes[pos].Type != LexemeType.EOF)
{
args.Add(this.Expression(ref pos));
if (this.lexemes[pos].Type == LexemeType.Comma)
+ {
pos++;
+
+ this.EnsureNotLexemeType(LexemeType.CloseParen, pos);
+ }
}
-
- // TODO: Check the arg count and execute with func with args.
-
+
this.EnsureLexemeType(LexemeType.CloseParen, pos);
pos++; // Move to just after the FuncCall.
- return this.Execute(funcName);
+ return this.Execute(funcName, args);
}
/// <summary>
View
@@ -25,8 +25,8 @@ public enum ScriptError
VariableStackError,
/// <summary>
- /// No function exists by the given name.
+ /// Failed to call a function.
/// </summary>
- FunctionDoesNotExist
+ InvalidFunctionCall
}
}
View
@@ -8,13 +8,13 @@ public class Variable
public object Val
{
get;
- set;
+ private set;
}
public VariableType Type
{
get;
- set;
+ private set;
}
public Variable()
@@ -29,7 +29,7 @@ public class VariableStack
// If we have a function stack, add it to that.
if (this.stack.Count > 0)
{
- this.stack[stack.Count - 1].Add(name, variable);
+ this.stack[this.stack.Count - 1].Add(name, variable);
}
else // Otherwise add it to the globals.
{
@@ -1,12 +1,11 @@
func Main()
{
$name = 'smack0007';
- echo $name;
-
- Test();
+ SaySomething($name);
+ SaySomething("Hello World!");
}
-func Test()
+func SaySomething($text)
{
- echo 'Test';
+ echo $text;
}

0 comments on commit 5239aeb

Please sign in to comment.