Skip to content
Closed
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
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,22 @@ package main
test::publicFn();
```

### Structs
```js
package main

pub struct Point {
var x i32;
var y i32;
}

var p = new Point();
p.x = 2;
p.y = 2;

print(p.x); // 2
```

### Supports

- [x] Int, bool, char, string, double, null
Expand Down
8 changes: 8 additions & 0 deletions Yail.Shared/Abstract/IInstantiable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Yail.Shared.Abstract;

public interface IInstantiable
{
void Set(string variableName, ValueObj value);
Dictionary<string, ValueObj> Get();
ValueObj Get(string propName);
}
33 changes: 33 additions & 0 deletions Yail.Shared/Objects/StructObj.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Yail.Shared.Abstract;

namespace Yail.Shared.Objects;

public class StructObj : ValueObj, IInstantiable
{
public StructObj()
{
Value = new Dictionary<string, ValueObj>();
}

public required string Name { get; set; }
public required bool IsPublic { get; set; }

public void Set(string variableName, ValueObj value)
{
var list = Get();

list.TryAdd(variableName, value);

Value = list;
}

public Dictionary<string, ValueObj> Get()
{
return Value as Dictionary<string, ValueObj>;

Check warning on line 26 in Yail.Shared/Objects/StructObj.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.
}

public ValueObj Get(string propName)
{
return (Value as Dictionary<string, ValueObj>)[propName];

Check warning on line 31 in Yail.Shared/Objects/StructObj.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
}
}
25 changes: 25 additions & 0 deletions Yail.Shared/ValueObj.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,31 @@ public ValueObj(object? value, EDataType dataType, bool isConst = false)
DataType = dataType;
IsConst = isConst;
}

public ValueObj(EDataType dataType, bool isConst = false)
{
DataType = dataType;
IsConst = isConst;

switch (dataType)
{
case EDataType.Int32:
Value = 0;
break;
case EDataType.Boolean:
Value = false;
break;
case EDataType.String:
Value = string.Empty;
break;
case EDataType.Double:
Value = 0.0D;
break;
default:
Value = null;
break;
}
}

public bool IsConst { get; set; }
public object? Value { get; set; }
Expand Down
43 changes: 43 additions & 0 deletions Yail.Tests/IOTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using Antlr4.Runtime;
using Yail.Grammar;

namespace Yail.Tests;

public class IOTests
{
private string RunCode(string code, string input = "")
{
using var output = new StringWriter();
Console.SetOut(output);

using var inputReader = new StringReader(input);
Console.SetIn(inputReader);

var inputStream = new AntlrInputStream(code);
var lexer = new ExpressionsLexer(inputStream);
var tokenStream = new CommonTokenStream(lexer);
var parser = new ExpressionsParser(tokenStream);

var tree = parser.program();

var visitor = new ExpressionsVisitor();
visitor.Visit(tree);

return output.ToString();
}

[Test]
public void TestInputOutput()
{
var code = @"
println(""Enter your name:"");
var name = input();
println(""Hello, "" + name + ""!"");
";

var simulatedInput = "Bob";
var output = RunCode(code, simulatedInput);

Assert.That(output, Is.EqualTo("Enter your name:\r\nHello, Bob!\r\n"));
}
}
98 changes: 98 additions & 0 deletions Yail.Tests/OperationsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
using Antlr4.Runtime;
using Yail.Grammar;

namespace Yail.Tests;

[TestFixture]
public class OperationsTest
{
private string RunCode(string code)
{
using var output = new StringWriter();
Console.SetOut(output);

var inputStream = new AntlrInputStream(code);
var lexer = new ExpressionsLexer(inputStream);
var tokenStream = new CommonTokenStream(lexer);
var parser = new ExpressionsParser(tokenStream);

var tree = parser.program();

var visitor = new ExpressionsVisitor();
visitor.Visit(tree);

return output.ToString();
}

[Test]
public void TestAddition()
{
var code = @"
var x = 3;
var y = x + 3;
println(y);
";

var actual = RunCode(code);

Assert.That(actual, Is.EqualTo("6\r\n"));
}

[Test]
public void TestSubtraction()
{
var code = @"
var x = 5;
var y = x - 3;
println(y);
";

var actual = RunCode(code);

Assert.That(actual, Is.EqualTo("2\r\n"));
}

[Test]
public void TestMultiplication()
{
var code = @"
var x = 5;
var y = x * 3;
println(y);
";

var actual = RunCode(code);

Assert.That(actual, Is.EqualTo("15\r\n"));
}

[Test]
public void TestDivision()
{
var code = @"
var x = 20;
var y = x / 4;
println(y);
";

var actual = RunCode(code);

Assert.That(actual, Is.EqualTo("5\r\n"));
}

[Test]
public void TestModulo()
{
var code = @"
var x = 20;
var y = x % 2;
var z = 15 % 2;
println(y);
println(z);
";

var actual = RunCode(code);

Assert.That(actual, Is.EqualTo("0\r\n1\r\n"));
}
}
18 changes: 0 additions & 18 deletions Yail.Tests/UnitTest1.cs

This file was deleted.

Loading
Loading