Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .github/workflows/JavaScript.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ name: CI Javascript
on:
# Triggers the workflow on push or pull request events but only for the "main" branch
push:
branches: [ "main" ]
branches: ["main"]
paths:
- "JavaScript/**"
- "grammar/*.g4"
pull_request:
branches: ["main"]
paths:
- "JavaScript/**"
- "grammar/*.g4"
Expand Down
9 changes: 8 additions & 1 deletion CSharp/Interpreter/ArcscriptFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public class Functions
{ "round", typeof (int) },
{ "min", typeof (double) },
{ "max", typeof (double) },
{ "visits", typeof (int) }
{ "visits", typeof (int) },
{ "resetVisits", typeof (void) },
};

public Functions(string elementId, IProject project, ArcscriptState state) {
Expand All @@ -46,6 +47,7 @@ public Functions(string elementId, IProject project, ArcscriptState state) {
this.functions["min"] = this.Min;
this.functions["max"] = this.Max;
this.functions["visits"] = this.Visits;
this.functions["resetVisits"] = this.ResetVisits;
}

public object Sqrt(IList<object> args) {
Expand Down Expand Up @@ -194,5 +196,10 @@ public object Visits(IList<object> args) {
IElement element = this._project.ElementWithId(elementId);
return element.Visits;
}

public object ResetVisits(IList<object> args) {
this.state.ResetVisits();
return null;
}
}
}
2 changes: 2 additions & 0 deletions CSharp/Interpreter/ArcscriptParserBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public ArcscriptParserBase(ITokenStream input) : base(input) {
{ "show", new FunctionArgs { MinArgs=1 } },
{ "reset", new FunctionArgs { MinArgs=1 } },
{ "resetAll", new FunctionArgs { MinArgs=0 } },
{ "resetVisits", new FunctionArgs { MinArgs=0, MaxArgs = 0} },
};

this.ArcscriptFunctions = functions;
Expand All @@ -54,6 +55,7 @@ public ArcscriptParserBase(ITokenStream input, TextWriter output, TextWriter err
{ "show", new FunctionArgs { MinArgs=1 } },
{ "reset", new FunctionArgs { MinArgs=1 } },
{ "resetAll", new FunctionArgs { MinArgs=0 } },
{ "resetVisits", new FunctionArgs { MinArgs=0, MaxArgs = 0} },
};

this.ArcscriptFunctions = functions;
Expand Down
21 changes: 20 additions & 1 deletion CSharp/Interpreter/ArcscriptState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,21 @@ public class ArcscriptState
public ArcscriptOutputs Outputs;
public string currentElement { get; set; }
public IProject project { get; set; }
public ArcscriptState(string elementId, IProject project)

private System.Action<string> _emit;
public ArcscriptState(string elementId, IProject project, System.Action<string>? emit = null)
{
Outputs = new ArcscriptOutputs();
this.currentElement = elementId;
this.project = project;
if (emit != null)
{
_emit = emit;
}
else
{
_emit = (string eventName) => { };
}
}

public IVariable GetVariable(string name) {
Expand Down Expand Up @@ -43,5 +53,14 @@ public void SetVarValues(string[] names, string[] values) {
this.VariableChanges[names[i]] = values[i];
}
}

public void ResetVisits()
{
foreach (var projectElement in project.Elements)
{
projectElement.Value.Visits = 0;
}
_emit("resetVisits");
}
}
}
14 changes: 12 additions & 2 deletions CSharp/Interpreter/ArcscriptTranspiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,20 @@ public class AwInterpreter
private IProject Project { get; set; }
private string ElementId { get; set; }

public AwInterpreter(IProject project, string elementId = "")
private System.Action<string> _emit;

public AwInterpreter(IProject project, string elementId = "", System.Action<string>? onEvent = null)
{
this.Project = project;
this.ElementId = elementId;
if (onEvent != null)
{
_emit = onEvent;
}
else
{
_emit = (string eventName) => { };
}
}

private ArcscriptParser.InputContext GetParseTree(string code)
Expand Down Expand Up @@ -59,7 +69,7 @@ public TranspilerOutput RunScript(string code)
throw new ParseException(e.Message, e);
}

ArcscriptVisitor visitor = new ArcscriptVisitor(this.ElementId, this.Project);
ArcscriptVisitor visitor = new ArcscriptVisitor(this.ElementId, this.Project, _emit);
object result;
try
{
Expand Down
13 changes: 11 additions & 2 deletions CSharp/Interpreter/ArcscriptVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,20 @@ public class ArcscriptVisitor : ArcscriptParserBaseVisitor<object>
public readonly ArcscriptState state;
public string elementId;
private readonly Functions _functions;
public ArcscriptVisitor(string elementId, IProject project) {
private System.Action<string> _emit;
public ArcscriptVisitor(string elementId, IProject project, System.Action<string>? emit = null) {
this.elementId = elementId;
this.project = project;
this.state = new ArcscriptState(elementId, project);
this.state = new ArcscriptState(elementId, project, emit);
this._functions = new Functions(elementId, project, this.state);
if (emit != null)
{
_emit = emit;
}
else
{
_emit = (string eventName) => { };
}
}

public override object VisitInput([NotNull] ArcscriptParser.InputContext context) {
Expand Down
2 changes: 2 additions & 0 deletions CSharp/Interpreter/INodes/IProject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#else
using System.Collections.Generic;
#endif
using Arcweave.Project;

namespace Arcweave.Interpreter.INodes
{
Expand All @@ -13,6 +14,7 @@ public interface IProject
#else
public List<Arcweave.Project.Variable> Variables { get; }
#endif
public Dictionary<string, Element> Elements { get; }
public Arcweave.Project.Element ElementWithId(string id);

public Arcweave.Project.Variable GetVariable(string name);
Expand Down
2 changes: 1 addition & 1 deletion CSharp/Project/Project.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public partial class Project
{
public List<Variable> Variables { get; }
public Dictionary<string, Element> Elements;
public Dictionary<string, Element> Elements { get; }
public Project(List<Variable> variables, Dictionary<string, Element> elements)
{
Variables = variables;
Expand Down
7 changes: 7 additions & 0 deletions CSharp/TestObjects.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ public struct TestVariable
public object value;
}

public struct EventData
{
public string name;
public Dictionary<string, object> args;
}

public struct TestCase
{
public string code;
Expand All @@ -17,6 +23,7 @@ public struct TestCase
public object result;
public string? error;
public Dictionary<string, int>? visits;
public List<EventData>? events;
}

public struct TestFile
Expand Down
21 changes: 20 additions & 1 deletion CSharp/UnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,15 @@ public void ValidTests(Dictionary<string, Variable> variables, TestCase testCase

Console.WriteLine("Testing Case: " + testCase.code);
var project = new Project.Project(variables.Values.ToList(), elements);
var i = new AwInterpreter(project, testCase.elementId);
List<EventData> events = new List<EventData>();
var onEvent = (string eventName) =>
{
var eventData = new EventData();
eventData.name = eventName;
eventData.args = new Dictionary<string, object>();
events.Add(eventData);
};
var i = new AwInterpreter(project, testCase.elementId, onEvent);
var output = i.RunScript(testCase.code);

if (testCase.output != null)
Expand All @@ -139,6 +147,17 @@ public void ValidTests(Dictionary<string, Variable> variables, TestCase testCase
{
Assert.That(output.Changes, Is.EqualTo(changesByName));
}

if (testCase.events != null)
{
Assert.That(events.Count, Is.EqualTo(testCase.events.Count));
for (int index = 0; index < events.Count; index++)
{
var expectedEvent = testCase.events[index];
Assert.That(events[index].name, Is.EqualTo(expectedEvent.name));
Assert.That(events[index].args, Is.EqualTo(expectedEvent.args));
}
}
}

[Test]
Expand Down
16 changes: 16 additions & 0 deletions CSharp/__tests__/valid.json
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,22 @@
"elementId": "b",
"result": false
},
{
"code": "<pre><code>resetVisits()</code></pre><pre><code>x = visits()</code></pre>",
"visits": {
"a": 3
},
"elementId": "a",
"changes": {
"var1": 0
},
"events": [
{
"name": "resetVisits",
"args": {}
}
]
},
{
"code": "<p>this</p><p>text</p><p>must</p><p>not</p><p>be</p><p>in</p><p>one</p><p>paragraph</p>",
"output": "<p>this</p><p>text</p><p>must</p><p>not</p><p>be</p><p>in</p><p>one</p><p>paragraph</p>"
Expand Down
31 changes: 30 additions & 1 deletion Cpp/demo/ArcscriptTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ UVisit* getVisits(json initVisits) {
return visits;
}

std::vector<std::string> events;

void onEvent(const char* eventName) {
events.emplace_back(eventName);
}

void resetEvents() {
events.clear();
}

std::string test(json testCase, size_t caseIndex, UVariable* initVars, size_t initVarLen) {
std::stringstream errorOutput;

Expand All @@ -110,6 +120,8 @@ std::string test(json testCase, size_t caseIndex, UVariable* initVars, size_t in
visitsLen = testCase["visits"].size();
}

resetEvents();

bool hasError = false;
std::string errorType;
if (testCase.contains("error")) {
Expand All @@ -118,7 +130,7 @@ std::string test(json testCase, size_t caseIndex, UVariable* initVars, size_t in
}
UTranspilerOutput* result = nullptr;
try {
result = runScriptExport(code, currentElement, initVars, initVarLen, visits, visitsLen);
result = runScriptExport(code, currentElement, initVars, initVarLen, visits, visitsLen, onEvent);
} catch (RuntimeErrorException &e) {
if (!hasError) {
errorOutput << "Unexpected Runtime Error: " << e.what() << std::endl;
Expand Down Expand Up @@ -215,6 +227,23 @@ std::string test(json testCase, size_t caseIndex, UVariable* initVars, size_t in
}
}

if (testCase.contains("events")) {
std::vector<std::string> expectedEvents;
for (auto event_json: testCase["events"]) {
expectedEvents.push_back(event_json["name"].get<std::string>());
}

if (expectedEvents.size() != events.size()) {
errorOutput << "Event count mismatch: expected " << expectedEvents.size() << ", got " << events.size() << std::endl;
} else {
for (size_t i = 0; i < expectedEvents.size(); i++) {
if (expectedEvents[i] != events[i]) {
errorOutput << "Event mismatch at index " << i << ": expected \"" << expectedEvents[i] << "\", got \"" << events[i] << "\"" << std::endl;
}
}
}
}

if (testCase.contains("result")) {
bool expectedResult = testCase["result"].get<bool>();
if (result->conditionResult != expectedResult) {
Expand Down
16 changes: 16 additions & 0 deletions Cpp/demo/tests/valid.json
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,22 @@
"elementId": "b",
"result": false
},
{
"code": "<pre><code>resetVisits()</code></pre><pre><code>x = visits()</code></pre>",
"visits": {
"a": 3
},
"elementId": "a",
"changes": {
"var1": 0
},
"events": [
{
"name": "resetVisits",
"args": {}
}
]
},
{
"code": "<p>this</p><p>text</p><p>must</p><p>not</p><p>be</p><p>in</p><p>one</p><p>paragraph</p>",
"output": "<p>this</p><p>text</p><p>must</p><p>not</p><p>be</p><p>in</p><p>one</p><p>paragraph</p>"
Expand Down
10 changes: 10 additions & 0 deletions Cpp/src/ArcscriptFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ namespace Arcweave {
{ "sqr", { 1, 1 } },
{ "sqrt", { 1, 1 } },
{ "visits", { 0, 1 } },
{ "resetVisits", { 0, 0 } },
};

std::any ArcscriptFunctions::Call(std::string functionName, std::vector<std::any> _args) {
Expand Down Expand Up @@ -115,6 +116,9 @@ namespace Arcweave {
else if (functionName == "visits") {
result = this->Visits(args);
}
else if (functionName == "resetVisits") {
result = this->ResetVisits(args);
}
return result;
}

Expand Down Expand Up @@ -265,4 +269,10 @@ namespace Arcweave {

return _state->visits[nodeId];
}

std::any ArcscriptFunctions::ResetVisits(std::vector<std::any> args) {
_state->resetVisits();
return {};
}

}
1 change: 1 addition & 0 deletions Cpp/src/ArcscriptFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@ class ArcscriptFunctions {
std::any Sqr(std::vector<std::any> args);
std::any Sqrt(std::vector<std::any> args);
std::any Visits(std::vector<std::any> args);
std::any ResetVisits(std::vector<std::any> args);
};
}
Loading
Loading