Skip to content

Commit 94d0afc

Browse files
committed
working on #36
1 parent 087fae8 commit 94d0afc

3 files changed

Lines changed: 37 additions & 7 deletions

File tree

7Sharp/Intrerpreter/Nodes/FunctionCallNode.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,41 @@ internal class FunctionCallNode : Node
1313
private readonly UserFunction userFunc;
1414
private readonly bool isUserFunc;
1515
private readonly List<List<Token<TokenType>>> args;
16+
private string name;
1617

1718
public FunctionCallNode(UserFunction userFunc, List<List<Token<TokenType>>> args, LexerPosition linePosition) : base(linePosition)
1819
{
1920
func = null;
2021
this.userFunc = userFunc;
2122
isUserFunc = true;
2223
this.args = args;
24+
name = userFunc.Name;
2325
}
2426

2527
public FunctionCallNode(_7sFunction func, List<List<Token<TokenType>>> args, LexerPosition linePosition) : base(linePosition)
2628
{
2729
this.func = func ?? throw new InterpreterException($"Unknown function at {linePosition}");
2830
this.args = args;
31+
isUserFunc = false;
32+
name = func.Name;
2933
}
3034

3135
public override void Run(ref InterpreterState state)
3236
{
33-
if (isUserFunc)
37+
try
3438
{
35-
userFunc.Run(ref state, ParseArgs(args, state));
39+
if (isUserFunc)
40+
{
41+
userFunc.Run(ref state, ParseArgs(args, state));
42+
}
43+
else
44+
{
45+
state.ReturnValue = func.Run(ParseArgs(args, state));
46+
}
3647
}
37-
else
48+
catch (Exception e)
3849
{
39-
state.ReturnValue = func.Run(ParseArgs(args, state));
50+
throw new InterpreterException($"Error in {name}", e);
4051
}
4152
}
4253

7Sharp/Intrerpreter/Sysfunctions.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,25 @@ namespace _7Sharp.Intrerpreter
77
{
88
internal class SysFunctions
99
{
10-
public static double Double(object value) => Convert.ToDouble(value);
10+
public static double Double(object value)
11+
{
12+
try
13+
{
14+
return Convert.ToDouble(value);
15+
}
16+
catch (OverflowException)
17+
{
18+
throw new InterpreterException("double: Number is too big!");
19+
}
20+
catch (FormatException)
21+
{
22+
throw new InterpreterException("double: Invalid number!");
23+
}
24+
catch (Exception e)
25+
{
26+
throw new InterpreterException("double: Unkown error", e);
27+
}
28+
}
1129

1230
[ManualDocs("write", "{\"title\":\"write(value)\",\"sections\":[{\"header\":\"Syntax\",\"text\":[{\"text\":\"Outputs \"},{\"text\":\"value\",\"color\":\"Green\"},{\"text\":\" to the console, followed by a newline.\"}]}]}")]
1331
public static void Write(object obj) => Console.WriteLine(obj);

7Sharp/Utils.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,10 @@ internal static string FormatPascalString(string v)
5151

5252
internal static void PrintError(Exception e)
5353
{
54-
if (e is InterpreterException)
54+
bool hasInnerException = e.InnerException != null;
55+
if (e is InterpreterException || (hasInnerException ? e.InnerException is InterpreterException : false))
5556
{
56-
ColorConsoleMethods.WriteLineColor($"{e.Message}", ConsoleColor.Red);
57+
ColorConsoleMethods.WriteLineColor($"{(hasInnerException ? e.InnerException.Message : e.Message)}", ConsoleColor.Red);
5758
return;
5859
}
5960
ColorConsoleMethods.WriteLineColor($"{e.GetType()}: {e.Message}\n{e.StackTrace}", ConsoleColor.Red);

0 commit comments

Comments
 (0)