Skip to content

Commit

Permalink
added basic try/catch/finally/throw exception handling support
Browse files Browse the repository at this point in the history
  • Loading branch information
bizzehdee committed Sep 26, 2020
1 parent 4d43704 commit e8b239f
Show file tree
Hide file tree
Showing 10 changed files with 225 additions and 7 deletions.
23 changes: 22 additions & 1 deletion DScript.Demo/testscript.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,25 @@ var myRegex = /see (chapter \d+(\.\d)*)/i;
var matches = testString.match(myRegex);
console.log(matches[1]);

console.log(p);
console.log(p);

try {

var x = 1;
throw "this broke";
}
catch (ex) {
var pns = "Exception message: " + ex;
console.log(pns);
}

try {

var x = 1;
}
catch (ex) {

}
finally {

}
10 changes: 8 additions & 2 deletions DScript.Test/DScript.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

<ItemGroup>
<PackageReference Include="nunit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.15.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
</ItemGroup>

<ItemGroup>
Expand Down Expand Up @@ -139,6 +139,12 @@
<None Update="Tests\test036.js">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Tests\test037.js">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Tests\test038.js">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
27 changes: 27 additions & 0 deletions DScript.Test/ScriptVarTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Text;

namespace DScript.Test
{
public class ScriptVarTests
{
[Test]
public void EmptyScriptVarIsUndefined()
{
var v = new ScriptVar();

Assert.IsTrue(v.IsUndefined);
}

[Test]
public void BoolScriptVarIsIntAndTrue()
{
var v = new ScriptVar(true);

Assert.IsTrue(v.IsInt);
Assert.IsTrue(v.Bool);
}
}
}
14 changes: 14 additions & 0 deletions DScript.Test/Tests/test037.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function throwsException() {
throw "Some Exception Happened";
}

try {
throwsException();
} catch (e) {

result = 0;
}
finally {

result = 1;
}
11 changes: 11 additions & 0 deletions DScript.Test/Tests/test038.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function throwsException() {
throw "Some Exception Happened";
}

try {
throwsException();
} catch (e) {

}

result = 1;
20 changes: 20 additions & 0 deletions DScript/JITException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace DScript
{
class JITException : Exception
{
public JITException()
{

}

public JITException(string message) :
base(message)
{

}
}
}
98 changes: 98 additions & 0 deletions DScript/ScriptEngine.Statement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,104 @@ private void Statement(ref bool execute)
}
}
}
else if(currentLexer.TokenType == ScriptLex.LexTypes.RTry)
{
var tryBlock = ParseDefinition(ScriptLex.LexTypes.RTry);
ScriptVarLink catchBlock = null, finallyBlock = null;

var originalLexer = currentLexer;

if (currentLexer.TokenType == ScriptLex.LexTypes.RCatch)
{
catchBlock = ParseDefinition(ScriptLex.LexTypes.RCatch);
}

if (currentLexer.TokenType == ScriptLex.LexTypes.RFinally)
{
finallyBlock = ParseDefinition(ScriptLex.LexTypes.RFinally);
}

try
{
var oldLex = currentLexer;
var newLex = new ScriptLex(tryBlock.Var.String);
currentLexer = newLex;

Block(ref execute);

execute = true;

currentLexer = oldLex;
}
catch(JITException ex)
{
if (catchBlock != null)
{
var catchScope = new ScriptVar(null, ScriptVar.Flags.Object);

var v = catchBlock?.Var?.FirstChild;
if(v != null)
{
catchScope.AddChild(v.Name, new ScriptVar(ex.Message));
}


var oldLex = currentLexer;
var newLex = new ScriptLex(catchBlock.Var.String);
currentLexer = newLex;

scopes.PushBack(catchScope);

Block(ref execute);

scopes.PopBack();

execute = true;

currentLexer = oldLex;
}
else
{
throw new ScriptException(ex.Message, ex);
}
}
finally
{
if(finallyBlock != null)
{
var oldLex = currentLexer;
var newLex = new ScriptLex(finallyBlock.Var.String);
currentLexer = newLex;

Block(ref execute);

execute = true;

currentLexer = oldLex;
}
}

currentLexer = originalLexer;
}
else if(currentLexer.TokenType == ScriptLex.LexTypes.RThrow)
{
currentLexer.Match(ScriptLex.LexTypes.RThrow);

var message = string.Empty;

if (currentLexer.TokenType == (ScriptLex.LexTypes)';')
{
currentLexer.Match((ScriptLex.LexTypes)';');
}
else
{

var res = Base(ref execute);
message = res.Var.String;
}

throw new JITException(message);
}
else
{
//execute a basic statement
Expand Down
15 changes: 12 additions & 3 deletions DScript/ScriptEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,13 @@ private ScriptVarLink FindInParentClasses(ScriptVar obj, string name)

private ScriptVarLink ParseFunctionDefinition()
{
currentLexer.Match(ScriptLex.LexTypes.RFunction);
var funcName = String.Empty;
return ParseDefinition(ScriptLex.LexTypes.RFunction);
}

private ScriptVarLink ParseDefinition(ScriptLex.LexTypes lexType)
{
currentLexer.Match(lexType);
var funcName = string.Empty;

//named function
if (currentLexer.TokenType == ScriptLex.LexTypes.Id)
Expand All @@ -327,7 +332,11 @@ private ScriptVarLink ParseFunctionDefinition()
}

var funcVar = new ScriptVarLink(new ScriptVar(null, ScriptVar.Flags.Function), funcName);
ParseFunctionArguments(funcVar.Var);

if (lexType == ScriptLex.LexTypes.RFunction || lexType == ScriptLex.LexTypes.RCatch)
{
ParseFunctionArguments(funcVar.Var);
}

var funcBegin = currentLexer.TokenStart;
var noExecute = false;
Expand Down
8 changes: 8 additions & 0 deletions DScript/ScriptLex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ public enum LexTypes
RUndefined,
RNew,
RTypeOf,
RTry,
RCatch,
RFinally,
RThrow,
RListEnd
}

Expand Down Expand Up @@ -249,6 +253,10 @@ public void GetNextToken()
case "undefined": TokenType = LexTypes.RUndefined; break;
case "new": TokenType = LexTypes.RNew; break;
case "typeof": TokenType = LexTypes.RTypeOf; break;
case "try": TokenType = LexTypes.RTry; break;
case "catch": TokenType = LexTypes.RCatch; break;
case "finally": TokenType = LexTypes.RFinally; break;
case "throw": TokenType = LexTypes.RThrow; break;
}
}
else if (CurrentChar.IsNumeric()) //Numbers
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
DScript
=======

Open sourced object oriented Javascript based scripting language implemented in C#.
Open sourced, object oriented, Javascript based, extendable scripting language implemented in C#.

***Example***

Expand All @@ -26,6 +26,7 @@ Open sourced object oriented Javascript based scripting language implemented in
- Method scope
- Arithmetic
- Eval/Exec
- Basic exception handling (try/catch/finally/throw)
- ...more

***Arithmetic operators***
Expand Down Expand Up @@ -65,3 +66,6 @@ Open sourced object oriented Javascript based scripting language implemented in
- tan
- tanh
- Random number library
- JSON
- parse
- stringify

0 comments on commit e8b239f

Please sign in to comment.