diff --git a/examples/HelloWorld.juk b/examples/HelloWorld.juk index f10e6993..2dd600f0 100644 --- a/examples/HelloWorld.juk +++ b/examples/HelloWorld.juk @@ -1,4 +1,4 @@ -func main() = { +sub main() = { // Print Hello World printLine("Hello World"); } diff --git a/examples/classtest.juk b/examples/classtest.juk index e2c9da69..677e37f5 100644 --- a/examples/classtest.juk +++ b/examples/classtest.juk @@ -1,12 +1,12 @@ class x(var initializer) = { var z = initializer; - func m() = { + sub m() = { return z; } } -func y() = { +sub y() = { var d = new x(32); printLine(d.m()); } diff --git a/examples/dontest.juk b/examples/dontest.juk index 11fcdc6c..53017771 100644 --- a/examples/dontest.juk +++ b/examples/dontest.juk @@ -1,23 +1,23 @@ -func main() = { +sub main() = { foo(); printLine("main"); var bb = testClass(); } -func process() = { +sub process() = { printLine("process"); } -func foo() = { +sub foo() = { process(); printLine("foo"); } class testClass = { - func one() = { + sub one() = { } - func two() = { + sub two() = { } } \ No newline at end of file diff --git a/examples/scanner.juk b/examples/scanner.juk index ca8d41f8..490a1860 100644 --- a/examples/scanner.juk +++ b/examples/scanner.juk @@ -1,4 +1,4 @@ -func main()={ +sub main()={ if ( 2<3 ) { printLine("foo"); @@ -9,7 +9,7 @@ func main()={ } } -func foo() = { +sub foo() = { var x = 3; while (x<=4) { @@ -18,7 +18,7 @@ func foo() = { } } -func three()={ +sub three()={ 3+3 x=function(3+3); x=1++==4 diff --git a/examples/test.juk b/examples/test.juk index e61ffb4c..691da385 100644 --- a/examples/test.juk +++ b/examples/test.juk @@ -1,10 +1,10 @@ -func process() = +sub process() = { printLine("process"); } -func main() = +sub main() = { printLine("main"); process(); diff --git a/examples/test2.juk b/examples/test2.juk index 0c65c6e3..3cf33c22 100644 --- a/examples/test2.juk +++ b/examples/test2.juk @@ -1,7 +1,7 @@ -func testC_sharp() = { +sub testC_sharp() = { printLine("AsdfA"); } -func main() = { +sub main() = { testC_sharp(); } \ No newline at end of file diff --git a/src/Juka/REPL.cs b/src/Juka/REPL.cs index 7a8d8012..ea6987b9 100644 --- a/src/Juka/REPL.cs +++ b/src/Juka/REPL.cs @@ -8,10 +8,11 @@ namespace Juka public class Repl { private static Compiler? compiler; - private static Stack funcData = new(); + private static Stack subData = new(); + private static readonly Stack redoStack = new(); private static string? dataStart; private static string? dataEnd; - private static bool isFuncOrClass; + private static bool isSubOrClass; public static async Task RunRepl() { @@ -46,11 +47,11 @@ private static async Task InitializeRepl() await SelfUpdate.Check(); - isFuncOrClass = false; - dataStart = "func main() = {"; + isSubOrClass = false; + dataStart = "sub main() = {"; dataEnd = "}"; - funcData = new Stack(); + subData = new Stack(); AnsiConsole.Write(new FigletText("Juka").Color(Color.Purple)); AnsiConsole.MarkupLine("[bold yellow]Hello[/] and [bold red]Welcome to 🍲 Juka Programming Language![/] For info visit [link blue]https://jukalang.com[/]. Type [bold palegreen1]!menu[/] to see options"); @@ -90,6 +91,9 @@ private static async Task HandleCommandAsync(string command) case "!undo": UndoLastCommand(); break; + case "!redo": + RedoLastCommand(); + break; case "!update": await UpdateJuka(); break; @@ -107,25 +111,25 @@ private static async Task HandleCommandAsync(string command) private static void HandleCode(string code) { - if (code.StartsWith("func") || code.StartsWith("class")) + if (code.StartsWith("sub") || code.StartsWith("class")) { - isFuncOrClass = true; - funcData.Push(code); - Trace.WriteLine("Starting Func: " + code); + isSubOrClass = true; + subData.Push(code); + Trace.WriteLine("Starting Subroutine: " + code); } - else if (isFuncOrClass) + else if (isSubOrClass) { if (code.StartsWith("}", StringComparison.OrdinalIgnoreCase)) { - funcData.Push(code); - Trace.WriteLine("Ending Func: " + code); - ExecuteFunction(); - isFuncOrClass = false; + subData.Push(code); + Trace.WriteLine("Ending Subroutine: " + code); + ExecuteSub(); + isSubOrClass = false; } else { - Trace.WriteLine("Reading Func: " + code); - funcData.Push(code); + Trace.WriteLine("Reading Subroutine: " + code); + subData.Push(code); } } else @@ -136,7 +140,7 @@ private static void HandleCode(string code) code = ""; } - funcData.Push(code); + subData.Push(code); ExecuteLine(); } } @@ -150,13 +154,16 @@ private static void DisplayMenu() table.AddColumn(new TableColumn("Description")); // Add some rows + table.AddRow("!menu", "[yellow]Displays this menu[/]"); + table.AddRow("!clear", "[green]Clears the REPL[/]"); table.AddRow("!list", "[red]Lists the current code[/]"); - table.AddRow("!clear", "[green]Clears The REPL[/]"); + table.AddRow("!get", "[aqua]Get list of libraries for Juka[/]"); table.AddRow("!undo", "[blue]Undoes last entered command[/]"); + table.AddRow("!redo", "[red]Redoes the undone command[/]"); table.AddRow("!update", "[yellow]Update Juka to latest version[/]"); - table.AddRow("!restart", "[fuchsia]Restart Application[/]"); - table.AddRow("!get", "[aqua]Get List of Libraries for Juka[/]"); - table.AddRow("!exit", "[darkred_1]Exits REPL[/]"); + table.AddRow("!restart", "[fuchsia]Restart application[/]"); + + table.AddRow("!exit", "[yellow]Exits REPL[/]"); AnsiConsole.Write(table); DisplayPrompt(); } @@ -165,16 +172,16 @@ private static void ClearConsole() { Console.Clear(); compiler = new Compiler(); - isFuncOrClass = false; - funcData.Clear(); - dataStart = "func main() = {"; + isSubOrClass = false; + subData.Clear(); + dataStart = "sub main() = {"; dataEnd = "}"; DisplayPrompt(); } private static void ListCode() { - foreach (var data in funcData.Reverse()) + foreach (var data in subData.Reverse()) { Console.WriteLine(data); } @@ -190,11 +197,20 @@ private static void GetLibraries() private static void UndoLastCommand() { - var templine = funcData.Pop(); + var templine = subData.Pop(); + redoStack.Push(templine); AnsiConsole.MarkupLine("[bold red]Removed: [/]" + templine); DisplayPrompt(); } + private static void RedoLastCommand() + { + var templine = redoStack.Pop(); + subData.Push(templine); + AnsiConsole.MarkupLine("[bold green]Added: [/]" + templine); + DisplayPrompt(); + } + private static async Task UpdateJuka() { await SelfUpdate.Update(); @@ -213,15 +229,15 @@ private static void ExitRepl() Environment.Exit(0); } - private static void ExecuteFunction() + private static void ExecuteSub() { StringBuilder userDataToExecute = new(); - foreach (string item in funcData.Reverse()) + foreach (string item in subData.Reverse()) { userDataToExecute.Append(item); } - funcData = new Stack(); + subData = new Stack(); dataEnd += userDataToExecute.ToString(); DisplayPrompt(); @@ -229,7 +245,7 @@ private static void ExecuteFunction() private static void ExecuteLine() { - string codeToExecute = dataStart + funcData.Peek() + dataEnd; + string codeToExecute = dataStart + subData.Peek() + dataEnd; Trace.WriteLine(codeToExecute); string output = "Something went wrong! Please restart the application"; diff --git a/src/JukaApi/Program.cs b/src/JukaApi/Program.cs index 4a7612f4..6759abfa 100644 --- a/src/JukaApi/Program.cs +++ b/src/JukaApi/Program.cs @@ -79,10 +79,10 @@ static IResult ExecuteCode(string code) if (compiler.HasErrors()) { string errors = string.Join(Environment.NewLine, compiler.ListErrors()); - return Results.Json(new { errors, original = decoded }); + return Results.Ok(new { errors, original = decoded }); } - return Results.Json(new { output = outputValue, original = decoded }); + return Results.Ok(new { output = outputValue, original = decoded }); } static async Task ExecuteCodeFromBody(HttpRequest request) diff --git a/src/JukaAzureFunction/JukaAzureFunction.cs b/src/JukaAzureFunction/JukaAzureFunction.cs index e5c83668..a0a777a1 100644 --- a/src/JukaAzureFunction/JukaAzureFunction.cs +++ b/src/JukaAzureFunction/JukaAzureFunction.cs @@ -10,18 +10,12 @@ namespace JukaAzureFunction; -public class JukaAzureFunction +public class JukaAzureFunction(ILogger log) { - private readonly ILogger logger; + private readonly ILogger logger = log; - public JukaAzureFunction(ILogger log) - { - logger = log; - } - - [Function("JukaAzureFunction")] - [OpenApiOperation(operationId: "Run", tags: new[] { "name" })] + [OpenApiOperation(operationId: "Run", tags: "name" )] [OpenApiSecurity("function_key", SecuritySchemeType.ApiKey, Name = "Pass Code as Get or Post", In = OpenApiSecurityLocationType.Query)] [OpenApiParameter(name: "code", In = ParameterLocation.Query, Required = true, Type = typeof(string), Description = "Enter the **Code**")] [OpenApiResponseWithBody(statusCode: HttpStatusCode.OK, contentType: "text/plain", bodyType: typeof(string), Description = "The OK response")] diff --git a/src/JukaCompiler/Compiler.cs b/src/JukaCompiler/Compiler.cs index b93f397f..f3fc8de0 100644 --- a/src/JukaCompiler/Compiler.cs +++ b/src/JukaCompiler/Compiler.cs @@ -8,26 +8,18 @@ using JukaCompiler.Expressions; using JukaCompiler.SystemCalls; using JukaCompiler.Lexer; -using System.Linq; namespace JukaCompiler { - /* - * Main entry point into the compiler responsible for setting up DI container - * and calling parser and compiler (currently interpreter). - */ public class Compiler { - private ServiceProvider? serviceProvider = null; + private ServiceProvider serviceProvider; + private readonly HostBuilder hostBuilder; - public Compiler() - { - Initialize(); - } - internal void Initialize() + public Compiler() { - var hostBuilder = new HostBuilder(); + hostBuilder = new HostBuilder(); hostBuilder.ConfigureServices(services => { services.AddSingleton(); @@ -41,13 +33,10 @@ internal void Initialize() hostBuilder.Build(); } - // Run the Compiler (Step: 3) + public string Go(string data, bool isFile = true) { - if (serviceProvider == null) - { - throw new JRuntimeException("Service provider is not created"); - } + CheckServiceProvider(); try { @@ -96,25 +85,35 @@ private static void SetupMainMethodRuntimeHook(List statements, Resolver r var mainFunction = statements.OfType().FirstOrDefault(f => f.StmtLexemeName.Equals("main")) ?? throw new Exception("No main function is defined"); Lexeme lexeme = new(LexemeType.Types.IDENTIFIER, 0, 0); lexeme.AddToken("main"); - Expr.Variable functionName = new(lexeme); - Expr.Call call = new(functionName, false, []); + Expr.Variable subroutineName = new(lexeme); + Expr.Call call = new(subroutineName, false, []); Stmt.Expression expression = new(call); resolver.Resolve([expression]); } public bool HasErrors() { - return serviceProvider != null && serviceProvider.GetRequiredService().HasErrors(); + CheckServiceProvider(); + return serviceProvider.GetRequiredService().HasErrors(); } public List ListErrors() + { + CheckServiceProvider(); + return serviceProvider.GetRequiredService().ListErrors(); + } + + private void CheckServiceProvider() { if (serviceProvider == null) { - throw new JRuntimeException("Unable to initialize provider for errors"); + throw new JRuntimeException("Service provider is not created"); } - - return serviceProvider.GetRequiredService().ListErrors(); } } + public class CompilerException(string message, Exception innerException) : Exception(message, innerException) + { + } } + + diff --git a/src/JukaCompiler/Exceptions/CompilerError.cs b/src/JukaCompiler/Exceptions/CompilerError.cs index b16a58da..8e833f3c 100644 --- a/src/JukaCompiler/Exceptions/CompilerError.cs +++ b/src/JukaCompiler/Exceptions/CompilerError.cs @@ -2,7 +2,7 @@ { internal class CompilerError : ICompilerError { - internal List Errors = new List(); + internal List Errors = []; internal string sourceFileName = string.Empty; public CompilerError() diff --git a/src/JukaCompiler/Exceptions/ParserException.cs b/src/JukaCompiler/Exceptions/ParserException.cs index c3a34e06..ebb5e15c 100644 --- a/src/JukaCompiler/Exceptions/ParserException.cs +++ b/src/JukaCompiler/Exceptions/ParserException.cs @@ -4,7 +4,7 @@ namespace JukaCompiler.Exceptions { internal class ParserException : Exception { - internal ParserException(string? message, LexemeType lexemeType) + internal ParserException(string? message) : base(message) { } diff --git a/src/JukaCompiler/Expressions/Expr.cs b/src/JukaCompiler/Expressions/Expr.cs index 431e45cf..4637c3b3 100644 --- a/src/JukaCompiler/Expressions/Expr.cs +++ b/src/JukaCompiler/Expressions/Expr.cs @@ -25,7 +25,7 @@ internal interface IVisitor R VisitDeleteExpr(DeleteDeclarationExpr expr); } - internal abstract R Accept(Expr.IVisitor visitor); + internal abstract R Accept(IVisitor visitor); private Lexeme? expressionLexeme; internal Lexeme? initializerContextVariableName; @@ -223,7 +223,7 @@ internal override R Accept(IVisitor visitor) } internal class Unary : Expr { - private LexemeType.Types lexemeType; + private readonly LexemeType.Types lexemeType; internal Unary(Lexeme lex, LexemeType.Types lexemeType) { expressionLexeme = lex; diff --git a/src/JukaCompiler/Interpreter/JukaClass.cs b/src/JukaCompiler/Interpreter/JukaClass.cs index 07ef3208..65ef6d66 100644 --- a/src/JukaCompiler/Interpreter/JukaClass.cs +++ b/src/JukaCompiler/Interpreter/JukaClass.cs @@ -2,9 +2,9 @@ { internal class JukaClass : IJukaCallable { - private string name; - private JukaClass superClass; - private Dictionary methods; + private readonly string name; + private readonly JukaClass superClass; + private readonly Dictionary methods; internal JukaClass(string name, JukaClass superClass, Dictionary methods) { @@ -28,12 +28,9 @@ public int Arity() { // FIND METHOD is broken // Declaration is never set correctly. - JukaInstance? instance = new JukaInstance(this); + JukaInstance? instance = new(this); JukaFunction? initializer = FindMethod("main"); - if (initializer != null) - { - initializer.Bind(instance).Call(methodName, interpreter, arguments); - } + initializer?.Bind(instance).Call(methodName, interpreter, arguments); return instance; } diff --git a/src/JukaCompiler/Interpreter/JukaEnvironment.cs b/src/JukaCompiler/Interpreter/JukaEnvironment.cs index b8387ef9..535ec1df 100644 --- a/src/JukaCompiler/Interpreter/JukaEnvironment.cs +++ b/src/JukaCompiler/Interpreter/JukaEnvironment.cs @@ -6,7 +6,7 @@ namespace JukaCompiler.Interpreter internal class JukaEnvironment { private JukaEnvironment? enclosing; - private Dictionary values = new Dictionary(); + private readonly Dictionary values = []; internal JukaEnvironment() { @@ -40,7 +40,7 @@ internal JukaEnvironment(JukaEnvironment enclosing) internal void Assign(Lexeme? name, object? value) { - var nameAsString = name?.ToString() ?? throw new JRuntimeException("unable to get variable name"); + var nameAsString = name?.ToString() ?? throw new JRuntimeException("unable to get variable name: "+name); if (values.ContainsKey(nameAsString)) { diff --git a/src/JukaCompiler/Interpreter/JukaFunction.cs b/src/JukaCompiler/Interpreter/JukaFunction.cs index be56a8cc..b98dd499 100644 --- a/src/JukaCompiler/Interpreter/JukaFunction.cs +++ b/src/JukaCompiler/Interpreter/JukaFunction.cs @@ -1,14 +1,13 @@ using JukaCompiler.Exceptions; using JukaCompiler.Statements; -using System.Reflection; namespace JukaCompiler.Interpreter { internal class JukaFunction : IJukaCallable { - private Stmt.Function? declaration; - private JukaEnvironment? closure; - private bool isInitializer; + private readonly Stmt.Function declaration; + private readonly JukaEnvironment closure; + private readonly bool isInitializer; internal JukaFunction(Stmt.Function declaration, JukaEnvironment closure, bool isInitializer) { @@ -61,10 +60,10 @@ public int Arity() var parameterNameExpressionLexeme = declaration.Params[i].parameterName.ExpressionLexeme; if (parameterNameExpressionLexeme != null) { - string? name = parameterNameExpressionLexeme.ToString(); + string name = parameterNameExpressionLexeme.ToString(); if (string.IsNullOrEmpty(name)) { - throw new ArgumentException("Unable to call function"); + throw new ArgumentException("Unable to call function: "+methodName); } object? value = arguments[i]; diff --git a/src/JukaCompiler/Interpreter/JukaInstance.cs b/src/JukaCompiler/Interpreter/JukaInstance.cs index 434d64dc..afed4800 100644 --- a/src/JukaCompiler/Interpreter/JukaInstance.cs +++ b/src/JukaCompiler/Interpreter/JukaInstance.cs @@ -4,8 +4,8 @@ namespace JukaCompiler.Interpreter { internal class JukaInstance { - private JukaClass Class; - private Dictionary fields = new Dictionary(); + private readonly JukaClass Class; + private readonly Dictionary fields = []; internal JukaInstance(JukaClass Class) { diff --git a/src/JukaCompiler/Interpreter/JukaInterpreter.cs b/src/JukaCompiler/Interpreter/JukaInterpreter.cs index fe848638..51f23eb1 100644 --- a/src/JukaCompiler/Interpreter/JukaInterpreter.cs +++ b/src/JukaCompiler/Interpreter/JukaInterpreter.cs @@ -87,7 +87,7 @@ internal void ExecuteBlock(List statements, JukaEnvironment env) } Stmt Stmt.IVisitor.VisitBlockStmt(Stmt.Block stmt) { - ExecuteBlock(stmt.statements, new(this.environment)); + ExecuteBlock(stmt.statements, new(environment)); return new Stmt.DefaultStatement(); } @@ -220,10 +220,7 @@ private void PrintLiteralExpr(Expr expr, Action printAction) } } - private void PrintLiteral(Expr.Literal expr, Action printAction) - { - printAction(expr.ExpressionLexemeName); - } + private void PrintLiteral(Expr.Literal expr, Action printAction) => printAction(expr.ExpressionLexemeName); private void PrintLiteral(Expr.LexemeTypeLiteral expr, Action printAction) { @@ -642,7 +639,7 @@ private static bool IsEqual(object a, object b) switch (argumentsMap.Count) { case > 0: - currentStackFrame.AddVariables(argumentsMap, this); + currentStackFrame.AddVariables(argumentsMap); break; } diff --git a/src/JukaCompiler/Interpreter/Resolver.cs b/src/JukaCompiler/Interpreter/Resolver.cs index c5ace576..96380cdb 100644 --- a/src/JukaCompiler/Interpreter/Resolver.cs +++ b/src/JukaCompiler/Interpreter/Resolver.cs @@ -9,15 +9,15 @@ namespace JukaCompiler.Interpreter { internal class Resolver : Stmt.IVisitor, Expr.IVisitor { - private JukaInterpreter interpreter; + private readonly JukaInterpreter interpreter; private FunctionType currentFunction = FunctionType.NONE; private ClassType currentClass = ClassType.NONE; - private ServiceProvider? ServiceProvider; - private Stack> scopes = new(); - Dictionary processScope = new(); - private Stack blockScope = new(); - private ICompilerError? compilerError; - private string errorMessage = "Resolver error - message:{0}"; + private readonly ServiceProvider? ServiceProvider; + private readonly Stack> scopes = new(); + private readonly Dictionary processScope = []; + private readonly Stack blockScope = new(); + private readonly ICompilerError? compilerError; + private readonly string errorMessage = "Resolver error - message:{0}"; private enum FunctionType { @@ -65,13 +65,8 @@ private void Resolve(Expr expr) public object VisitAssignExpr(Expr.Assign expr) { - var frame = blockScope.Peek(); - if (frame == null) - { - throw new Exception(string.Format(errorMessage,"No stack frames")); - } - - if(processScope.TryGetValue(frame, out BlockScope? value)) + var frame = blockScope.Peek() ?? throw new Exception(string.Format(errorMessage,"No stack frames")); + if (processScope.TryGetValue(frame, out BlockScope? value)) { if (value != null && value.lexemeScope.TryGetValue(expr.ExpressionLexeme?.ToString()!, out var lexeme)) { @@ -100,11 +95,11 @@ public object VisitBlockStmt(Stmt.Block stmt) return new Stmt.DefaultStatement(); } - public object VisitCallExpr(Expr.Call expr) + public object VisitCallExpr(Call expr) { - if (expr is Expr.Call) + if (expr is not null) { - var call = (Expr.Call)expr; + Call call = expr; BeginScope(expr.callee.ExpressionLexeme?.ToString()!); if (expr.callee.ExpressionLexeme != null) { @@ -125,7 +120,7 @@ public object VisitClassStmt(Stmt.Class stmt) { ClassType enclosingClass = currentClass; currentClass = ClassType.CLASS; - BlockScope? blockScope = new BlockScope(); + BlockScope blockScope = new(); Declare(stmt.name); Define(stmt.name); @@ -153,8 +148,9 @@ public object VisitClassStmt(Stmt.Class stmt) scopes.Peek().Add("this", true); processScope.Add("this", blockScope); - foreach (Stmt.Function method in stmt.methods) + for (int i = 0; i < stmt.methods.Count; i++) { + Stmt.Function method = stmt.methods[i]; //FunctionType decl = FunctionType.METHOD; //implement ctor // if(method.ExpressionLexeme.ToString @@ -206,7 +202,7 @@ public object VisitGroupingExpr(Expr.Grouping expr) { if (expr == null || expr.expression == null) { - throw new ArgumentNullException("grouping has some null stuff"); + throw new ArgumentNullException("Grouping has some null stuff"); } Resolve(expr.expression); @@ -280,7 +276,7 @@ public object VisitReturnStmt(Stmt.Return stmt) public object VisitBreakStmt(Stmt.Break stmt) { - Stmt.Return returnStatement = new Stmt.Return(); + Stmt.Return returnStatement = new(); return VisitReturnStmt(returnStatement); } @@ -358,16 +354,16 @@ private void ResolveLocal(Expr expr, Lexeme name) { if (scopes.Peek().ContainsKey(name.ToString())) { - this.interpreter.Resolve(expr, scopes.Count() - 1 - i); + this.interpreter.Resolve(expr, scopes.Count - 1 - i); } } } public object VisitVarStmt(Stmt.Var stmt) { - if (stmt==null || stmt.name == null) + if (stmt == null || stmt.name == null) { - throw new ArgumentNullException("new vist stmt == null"); + throw new ArgumentNullException("new vist stmt == null"+stmt); } Declare(stmt.name); @@ -406,7 +402,7 @@ private void Declare(Lexeme name) return; } - BlockScope? bsObject = new BlockScope(); + BlockScope bsObject = new(); if (bsObject.lexemeScope.ContainsKey(name.ToString())) { throw new Exception("variable already exist"); @@ -419,7 +415,7 @@ private void Declare(Lexeme name) private void Define(Lexeme name) { - if (!scopes.Any() || (scopes.Peek().Count == 0)) + if (scopes.Count == 0 || (scopes.Peek().Count == 0)) { return; } @@ -441,12 +437,7 @@ private void ResolveFunction(Stmt.Function function, FunctionType type) foreach (var param in function.typeParameterMaps) { - var literalName = param.parameterName as Expr.Variable; - if (literalName == null) - { - throw new Exception("Something went wrong when resolving the function"); - } - + Variable literalName = param.parameterName as Expr.Variable ?? throw new Exception("Something went wrong when resolving the function"); if (literalName.ExpressionLexeme != null) { Declare(literalName.ExpressionLexeme); @@ -458,13 +449,16 @@ private void ResolveFunction(Stmt.Function function, FunctionType type) } } - Resolve(function.body); + if (function != null && function.body != null) + { + Resolve(function.body); + } currentFunction = enclosingFunction; } private void BeginScope(string scopeName) { - scopes.Push(new Dictionary()); + scopes.Push([]); blockScope.Push(scopeName); } @@ -477,7 +471,7 @@ private void EndScope() internal class BlockScope { - internal Dictionary> processScope = new(); - internal Dictionary lexemeScope = new(); + internal Dictionary> processScope = []; + internal Dictionary lexemeScope = []; } } diff --git a/src/JukaCompiler/Interpreter/StackFrame.cs b/src/JukaCompiler/Interpreter/StackFrame.cs index 0f095409..9a800466 100644 --- a/src/JukaCompiler/Interpreter/StackFrame.cs +++ b/src/JukaCompiler/Interpreter/StackFrame.cs @@ -7,34 +7,33 @@ namespace JukaCompiler.Interpreter { internal class StackFrame { - private Dictionary frameVariables = new(); - private string frameName; - private Dictionary variables = new(); - private Dictionary variableAndKind = new(); + private readonly Dictionary frameVariables = []; + private readonly string frameName; + private readonly Dictionary variables = []; + private readonly Dictionary variableAndKind = []; internal StackFrame(string name) { this.frameName = name; } - internal void AddVariables(Dictionary variables, JukaInterpreter interpreter) + internal void AddVariables(Dictionary variables) { foreach (var variable in variables) { string name = variable.Key; - object? value = null; + object? value; if (variable.Value is Expr.Literal) { value = variable.Value; + AddVariable(name, value, variable.Value.GetType(), null); } - else + else if(variable.Value != null) { value = ((Expr.LexemeTypeLiteral)variable.Value).literal; + AddVariable(name, value, variable.Value.GetType(), null); } - - // - AddVariable(name, value, variable.Value.GetType(), null); } } @@ -42,14 +41,8 @@ internal void AddVariables(Dictionary variables, JukaInterprete { if (variable.exprInitializer != null) { - object? variableValue = interpreter.Evaluate(variable.exprInitializer); - - if (variableValue == null) - { - throw new JRuntimeException("the value of the variable is null"); - } - - if (!(variable.exprInitializer is Expr.Call)) + object? variableValue = interpreter.Evaluate(variable.exprInitializer) ?? throw new JRuntimeException("The value of the variable is null"); + if (variable.exprInitializer is not Expr.Call) { string variableName = variable.name?.ToString() ?? throw new JRuntimeException("variable name is missing"); AddVariable(variableName, variableValue, variableValue.GetType(),variable.exprInitializer); @@ -58,7 +51,7 @@ internal void AddVariables(Dictionary variables, JukaInterprete return variableValue; } - throw new JRuntimeException("unable to add variable"); + throw new JRuntimeException("Unable to add variable to StackFrame"); } internal void AddVariable(string name, object? variableValue, Type? variableKind, Expr? expressionContext) @@ -94,13 +87,7 @@ internal bool UpdateVariable(string name, object? value) internal bool DeleteVariable(string name) { - if (variables.ContainsKey(name)) - { - variables.Remove(name); - return true; - } - - return false; + return variables.Remove(name); } internal bool TryGetStackVariableByName(string name, out StackVariableState? variable) @@ -122,7 +109,7 @@ internal class StackVariableState internal object? Value; internal Type? type; internal Expr? expressionContext; - internal object[] arrayValues; + internal object[] arrayValues = []; } } } diff --git a/src/JukaCompiler/Lexer/Keywords.cs b/src/JukaCompiler/Lexer/Keywords.cs index 4bde6af0..db2ff6ea 100644 --- a/src/JukaCompiler/Lexer/Keywords.cs +++ b/src/JukaCompiler/Lexer/Keywords.cs @@ -21,6 +21,7 @@ public enum KeyWordsEnum While, Debugger, Func, + Sub, This, With, Default, @@ -60,6 +61,7 @@ public class KeyWords public const string WHILE = "while"; public const string DEBUGGER = "debugger"; public const string FUNC = "func"; + public const string SUB = "sub"; public const string THIS = "this"; public const string WITH = "with"; public const string DEFAULT = "default"; @@ -80,10 +82,7 @@ public class KeyWords public const string LPAREN = "("; public const string RPAREN = ")"; - - - - public static List keyWordNames = new List() + private static readonly List list = new() { {"array"}, {"break"}, @@ -104,6 +103,7 @@ public class KeyWords {"while"}, {"debugger"}, {"function"}, + {"subroutine"}, {"this"}, {"with"}, {"default"}, @@ -122,8 +122,9 @@ public class KeyWords {"class"}, {"main"}, }; + public static List keyWordNames = list; - public static Dictionary keyValuePairs = new Dictionary() + public static Dictionary keyValuePairs = new() { { "array", KeyWordsEnum.Array }, { "break", KeyWordsEnum.Break }, @@ -143,7 +144,8 @@ public class KeyWords { "switch", KeyWordsEnum.Switch }, { "while", KeyWordsEnum.While }, { "debugger", KeyWordsEnum.Debugger }, - { "func", KeyWordsEnum.Func }, + { "func", KeyWordsEnum.Func }, + { "sub", KeyWordsEnum.Sub }, { "this", KeyWordsEnum.This }, { "with", KeyWordsEnum.With }, { "default", KeyWordsEnum.Default }, diff --git a/src/JukaCompiler/Lexer/Lexeme.cs b/src/JukaCompiler/Lexer/Lexeme.cs index 106cb9ac..dea2fc9f 100644 --- a/src/JukaCompiler/Lexer/Lexeme.cs +++ b/src/JukaCompiler/Lexer/Lexeme.cs @@ -39,6 +39,7 @@ internal enum Types ELSE, FALSE, FUNC, + SUB, FOR, IF, NULL, @@ -66,11 +67,11 @@ internal enum Types } internal class Lexeme { - private StringBuilder tokenBuilder = new StringBuilder(); + private readonly StringBuilder tokenBuilder = new(); private bool isKeyWord = false; private Int64 typeOfKeyWord; - private int lineNumber; - private int columnNumber; + private readonly int lineNumber; + private readonly int columnNumber; internal LexemeType.Types LexemeType { get; set; } diff --git a/src/JukaCompiler/Parse/Parser.cs b/src/JukaCompiler/Parse/Parser.cs index c3153801..c7ac6dde 100644 --- a/src/JukaCompiler/Parse/Parser.cs +++ b/src/JukaCompiler/Parse/Parser.cs @@ -10,11 +10,11 @@ namespace JukaCompiler.Parse { public class Parser { - private List tokens = new(); + private List tokens = []; private int current = 0; private Scanner? scanner; public ServiceProvider Services { get; } - private ICompilerError compilerError; + private readonly ICompilerError compilerError; internal Parser(Scanner scanner, ServiceProvider services) { @@ -32,7 +32,7 @@ internal List Parse() } tokens = scanner.Scan()!; - List statements = new(); + List statements = []; while(!IsAtEnd()) { statements.Add(Declaration()); @@ -89,9 +89,9 @@ private Stmt Declaration() } - if (Match(LexemeType.Types.FUNC)) + if (Match(LexemeType.Types.SUB)) { - return Function("func"); + return Function("sub"); } if (MatchKeyWord()) @@ -147,7 +147,7 @@ private Stmt Statement() return ExpressionStatement(); } - private Stmt IfStatement() + private Stmt.If IfStatement() { Consume(LexemeType.Types.LEFT_PAREN, Previous()); @@ -176,7 +176,7 @@ private Stmt IfStatement() throw new JRuntimeException("If statement failure"); } - private Stmt WhileStatement() + private Stmt.While WhileStatement() { Consume(LexemeType.Types.LEFT_PAREN, Previous()); @@ -189,7 +189,7 @@ private Stmt WhileStatement() return new Stmt.While(condition, whileBlock); } - private Stmt ForStatement() + private Stmt.For ForStatement() { Consume(LexemeType.Types.LEFT_PAREN, Previous()); if (MatchKeyWord()) @@ -217,7 +217,7 @@ private Stmt ForStatement() throw new JRuntimeException("no valid variable"); } - private Stmt BreakStatement() + private Stmt.Break BreakStatement() { Consume(LexemeType.Types.SEMICOLON, Previous()); var expr = Expr(); @@ -225,7 +225,7 @@ private Stmt BreakStatement() } - private Stmt ReturnStatement() + private Stmt.Return ReturnStatement() { var keyword = Previous(); Expr value = null!; @@ -244,14 +244,14 @@ private Stmt ReturnStatement() return new Stmt.Return(keyword, value); } - private Stmt ExpressionStatement() + private Stmt.Expression ExpressionStatement() { Expr expr = Expr(); Consume(LexemeType.Types.SEMICOLON, Peek()); return new Stmt.Expression(expr); } - private Stmt PrintLine() + private Stmt.PrintLine PrintLine() { Lexeme keyword = Previous(); Consume(LexemeType.Types.LEFT_PAREN, Peek()); @@ -264,7 +264,7 @@ private Stmt PrintLine() return new Stmt.PrintLine(value); } - private Stmt Print() + private Stmt.Print Print() { Lexeme keyword = Previous(); Consume(LexemeType.Types.LEFT_PAREN, Peek()); @@ -371,7 +371,7 @@ private bool CheckKeyWord() return false; } - private Stmt Function(string kind) + private Stmt.Function Function(string kind) { Lexeme name = Consume(LexemeType.Types.IDENTIFIER, Peek()); Consume(LexemeType.Types.LEFT_PAREN, Peek()); @@ -401,13 +401,13 @@ private Stmt Function(string kind) return stmt; } - private Stmt ClassDeclaration(string kind) + private Stmt.Class ClassDeclaration(string kind) { Lexeme name = Consume(LexemeType.Types.IDENTIFIER, Peek()); Consume(LexemeType.Types.EQUAL, Peek()); Consume(LexemeType.Types.LEFT_BRACE, Peek()); - List functions = new(); - List variableDeclarations = new(); + List functions = []; + List variableDeclarations = []; if(!Check(LexemeType.Types.RIGHT_BRACE)) { @@ -419,7 +419,7 @@ private Stmt ClassDeclaration(string kind) break; } - if (isFunc.LexemeType == LexemeType.Types.FUNC) + if (isFunc.LexemeType == LexemeType.Types.SUB) { functions.Add((Stmt.Function)Declaration()); } @@ -480,11 +480,11 @@ private Expr Assignment() //expr.ExpressionLexemeName = - if (expr is Expr.Variable && - ((Expr.Variable)expr) != null && - ((Expr.Variable)expr).ExpressionLexeme != null) + if (expr is Expr.Variable exprVariables && + exprVariables != null && + exprVariables.ExpressionLexeme != null) { - Expr.Variable variable = (Expr.Variable)expr; + Expr.Variable variable = exprVariables; //expr.ExpressionLexemeName = variable.ExpressionLexeme. if (variable != null && variable.ExpressionLexeme != null) { @@ -493,11 +493,11 @@ private Expr Assignment() //> Classes assign-set } - if (expr is Expr.Get && - ((Expr.Get)expr) != null && - ((Expr.Get)expr).ExpressionLexeme != null) + if (expr is Expr.Get exprGet && + exprGet != null && + exprGet.ExpressionLexeme != null) { - Expr.Get get = (Expr.Get)expr; + Expr.Get get = exprGet; if (get != null && get.ExpressionLexeme != null) { return new Expr.Set(get, get.ExpressionLexeme, value); @@ -615,9 +615,9 @@ private Expr Unary() if (Match(LexemeType.Types.BANG)) { - Lexeme op = Previous(); - Expr right = Unary(); - //******* return new Expr.Unary(op, right); + //Lexeme op = Previous(); + //Expr right = Unary(); + //return new Expr.Unary(op, right); } Lexeme idLexeme = Peek(); @@ -745,9 +745,9 @@ private Expr Primary() throw new Exception(Peek() + "Expect expr"); } - private Expr FinishCall(Expr callee) + private Expr.Call FinishCall(Expr callee) { - List arguments = new(); + List arguments = []; if (!Check(LexemeType.Types.RIGHT_PAREN)) { do diff --git a/src/JukaCompiler/Scan/Scanner.cs b/src/JukaCompiler/Scan/Scanner.cs index f74eb689..e1d10668 100644 --- a/src/JukaCompiler/Scan/Scanner.cs +++ b/src/JukaCompiler/Scan/Scanner.cs @@ -12,9 +12,9 @@ internal class Scanner private int current = 0; private int line = 1; private int column = 0; - private byte[] fileData; - private readonly List lexemes = new(); - private ICompilerError compilerError; + private readonly byte[] fileData; + private readonly List lexemes = []; + //private readonly ICompilerError compilerError; private static readonly Dictionary keywordsDictionary = new() { @@ -22,6 +22,7 @@ internal class Scanner { "class", LexemeType.Types.CLASS }, { "else", LexemeType.Types.ELSE }, { "func", LexemeType.Types.FUNC }, + { "sub", LexemeType.Types.SUB }, { "for", LexemeType.Types.FOR }, { "if", LexemeType.Types.IF }, { "null", LexemeType.Types.NULL }, @@ -50,12 +51,12 @@ internal class Scanner internal Scanner(string data, IServiceProvider serviceProvider, bool isFile = true) { - this.compilerError = serviceProvider.GetRequiredService(); + //this.compilerError = serviceProvider.GetRequiredService(); if (isFile) { if (string.IsNullOrEmpty(data)) { - throw new ArgumentNullException("The path is null"); + throw new ArgumentNullException("The path is null"+data); } if (!File.Exists(data)) @@ -255,7 +256,7 @@ internal void AddSymbol(char symbol, LexemeType.Types type) this.lexemes.Add(lex); } - internal bool TryGetKeyWord(Lexeme? lex) + internal static bool TryGetKeyWord(Lexeme? lex) { bool isKeyword = false; @@ -308,7 +309,7 @@ internal void Identifier() Advance(); } - var svalue = Encoding.Default.GetString(Memcopy(fileData, start, current)); + var svalue = Encoding.Default.GetString(Memcopy(fileData, start)); Lexeme? identifier = new(LexemeType.Types.IDENTIFIER, this.line, this.column); identifier.AddToken(svalue); @@ -326,7 +327,7 @@ internal void Number() temp = Peek(); } - var svalue = System.Text.Encoding.Default.GetString(Memcopy(fileData, start, current)); + var svalue = System.Text.Encoding.Default.GetString(Memcopy(fileData, start)); Lexeme? number = new(LexemeType.Types.NUMBER, this.line, this.column); number.AddToken(svalue); @@ -352,14 +353,14 @@ private void String() return; } - var svalue = System.Text.Encoding.Default.GetString(Memcopy(fileData, start + 1, current - 1)); + var svalue = Encoding.Default.GetString(Memcopy(fileData, start + 1)); Lexeme? s = new(LexemeType.Types.STRING, this.line, this.column); s.AddToken(svalue.ToString()); this.lexemes.Add(s); Advance(); } - private byte[] Memcopy(byte[] from, int start, int size) + private byte[] Memcopy(byte[] from, int start) { byte [] to = new byte[current - start]; for(int toIndex = 0, i = start; i < current; i++, toIndex++) diff --git a/src/JukaCompiler/Statements/Statement.cs b/src/JukaCompiler/Statements/Statement.cs index 0dbe1aa5..91b1fa65 100644 --- a/src/JukaCompiler/Statements/Statement.cs +++ b/src/JukaCompiler/Statements/Statement.cs @@ -23,7 +23,7 @@ internal interface IVisitor R VisitForStmt(For stmt); object VisitArrayExpr(Expr.ArrayDeclarationExpr expr); } - internal abstract R Accept(Stmt.IVisitor vistor); + internal abstract R Accept(IVisitor vistor); private Lexeme stmtLexeme = new(); internal Lexeme StmtLexeme @@ -39,29 +39,20 @@ internal string StmtLexemeName internal class Block : Stmt { - internal Block(List statements) - { - this.statements = statements; - } + internal Block(List statements) => this.statements = statements; - internal List? statements; - internal override R Accept(IVisitor vistor) - { - return vistor.VisitBlockStmt(this); - } + internal List statements; + internal override R Accept(IVisitor vistor) => vistor.VisitBlockStmt(this); } internal class Function : Stmt { internal List? body = []; internal List typeParameterMaps; - public override bool Equals(object? obj) - { - return this.StmtLexemeName.Equals(obj); - } + public override bool Equals(object? obj) => StmtLexemeName.Equals(obj); internal Function(Lexeme stmtLexeme, List parametersMap, List body) { - if (!(stmtLexeme.ToString().All(c => char.IsLetterOrDigit(c) || c == '_'))) + if (!stmtLexeme.ToString().All(c => char.IsLetterOrDigit(c) || c == '_')) { throw new Exception("Function {ExpressionLexeme.ToString()} has an invalid ExpressionLexeme"); } @@ -70,29 +61,20 @@ internal Function(Lexeme stmtLexeme, List parametersMap, List< this.body = body; } - internal override R Accept(IVisitor vistor) - { - return vistor.VisitFunctionStmt(this); - } + internal override R Accept(IVisitor vistor) => vistor.VisitFunctionStmt(this); - internal List Params - { - get { return this.typeParameterMaps; } - } + internal List Params => typeParameterMaps; - public override int GetHashCode() - { - return base.GetHashCode(); - } + public override int GetHashCode() => base.GetHashCode(); } internal class Class : Stmt { - internal Lexeme? name; - internal List? methods; - internal List? variableDeclarations; + internal Lexeme name; + internal List methods; + internal List variableDeclarations; internal Expr.Variable? superClass; - internal Class(Lexeme name, List methods, List variableDeclarations) + internal Class(Lexeme name, List methods, List variableDeclarations) { this.name = name; this.methods = methods; @@ -107,11 +89,11 @@ internal override R Accept(IVisitor vistor) } internal class Expression : Stmt { - internal Expr? Expr; + internal Expr Expr; internal Expression(Expr expr) { - this.Expr = expr; + Expr = expr; } internal override R Accept(IVisitor vistor) @@ -121,9 +103,9 @@ internal override R Accept(IVisitor vistor) } internal class If : Stmt { - internal Expr? condition; - internal Stmt? thenBranch; - internal Stmt? elseBranch; + internal Expr condition; + internal Stmt thenBranch; + internal Stmt elseBranch; internal If(Expr condition, Stmt thenBranch, Stmt elseBranch) { @@ -149,6 +131,7 @@ internal PrintLine(Expr expr) internal PrintLine() { + this.expr = null; } internal override R Accept(IVisitor vistor) @@ -167,6 +150,7 @@ internal Print(Expr expr) internal Print() { + this.expr = null; } internal override R Accept(IVisitor vistor) @@ -178,7 +162,7 @@ internal class Var : Stmt { internal Lexeme? name; internal Expr? exprInitializer; - internal bool? isInitalizedVar = false; + internal bool isInitalizedVar = false; internal Var(Lexeme name, Expr expr) { @@ -215,8 +199,8 @@ internal override R Accept(IVisitor vistor) internal class While : Stmt { - internal Expr? condition; - internal Stmt? whileBlock; + internal Expr condition; + internal Stmt whileBlock; internal While(Expr condition, Stmt whileBlock) { @@ -232,10 +216,10 @@ internal override R Accept(IVisitor vistor) internal class For : Stmt { - private Stmt.Var init; - private Expr breakExpr; - private Expr incExpr; - private Stmt forBody; + private readonly Var init; + private readonly Expr breakExpr; + private readonly Expr incExpr; + private readonly Stmt forBody; internal For(Stmt.Var init, Expr breakExpr, Expr incExpr, Stmt forBody) { @@ -267,6 +251,8 @@ internal Return(Lexeme keyword, Expr expr) internal Return() { + this.expr = null; + this.keyword = null; } internal override R Accept(IVisitor vistor) @@ -276,7 +262,7 @@ internal override R Accept(IVisitor vistor) } internal class Break : Stmt { - internal Expr? expr; + internal Expr expr; internal Break(Expr expr) { @@ -299,7 +285,7 @@ internal override R Accept(IVisitor vistor) internal class LiteralLexemeExpression : Stmt { - internal Expr.LexemeTypeLiteral? ltl; + internal Expr.LexemeTypeLiteral ltl; internal LiteralLexemeExpression(Expr.LexemeTypeLiteral lexemeTypeLiteral) { diff --git a/src/JukaUnitTest/ArithmeticUnitTest.cs b/src/JukaUnitTest/ArithmeticUnitTest.cs index c1e8e763..e9180417 100644 --- a/src/JukaUnitTest/ArithmeticUnitTest.cs +++ b/src/JukaUnitTest/ArithmeticUnitTest.cs @@ -11,7 +11,7 @@ public class ArithmeticUnitTest : UnitTestStructure public void Add(dynamic a, dynamic b, string expected) { SourceAsString += @" - func test_func() = { + sub test_func() = { var x=" + a + @"; var y=" + b + @"; var z=x+y; @@ -26,7 +26,7 @@ func test_func() = { [DataRow(-5, -5, "0")] public void Subtract(dynamic a, dynamic b, string expected) { - SourceAsString += @"func test_func() = { + SourceAsString += @"sub test_func() = { var x=" + a + @"; var y=" + b + @"; var z=x-y; print(z); }"; @@ -38,7 +38,7 @@ public void Subtract(dynamic a, dynamic b, string expected) [DataRow(-5, -5, "1")] // Expected result should be "1.0" for division public void Divide(dynamic a, dynamic b, string expected) { - SourceAsString += @"func test_func() = + SourceAsString += @"sub test_func() = { var x=" + a + @"; var y=" + b + @"; @@ -54,7 +54,7 @@ public void Divide(dynamic a, dynamic b, string expected) [DataRow(-5, -5, "25")] public void Multiply(dynamic a, dynamic b, string expected) { - SourceAsString += @"func test_func() = + SourceAsString += @"sub test_func() = { var x=" + a + @"; var y=" + b + @"; diff --git a/src/JukaUnitTest/CommentsUnitTest.cs b/src/JukaUnitTest/CommentsUnitTest.cs index 6e501347..1c4d1523 100644 --- a/src/JukaUnitTest/CommentsUnitTest.cs +++ b/src/JukaUnitTest/CommentsUnitTest.cs @@ -11,7 +11,7 @@ public class CommentsUnitTest : UnitTestStructure [DataRow(0, "0")] public void EmptyComment(dynamic value, string expected) { - SourceAsString += @"func test_func() = + SourceAsString += @"sub test_func() = { var y = " + value + @"; print(y); // Uncommented this line diff --git a/src/JukaUnitTest/CompilerUnitTest.cs b/src/JukaUnitTest/CompilerUnitTest.cs index 41a4bd2d..9effd5de 100644 --- a/src/JukaUnitTest/CompilerUnitTest.cs +++ b/src/JukaUnitTest/CompilerUnitTest.cs @@ -10,7 +10,7 @@ public class CompilerUnitTest : UnitTestStructure public void StackBasedArray() { SourceAsString += - @"func test_func() = + @"sub test_func() = { var x = array[3]; x[1] = ""test""; @@ -27,7 +27,7 @@ public void StackBasedArray() public void HeapBasedArray() { SourceAsString += - @"func test_func() = + @"sub test_func() = { var y = ""te""; print(y); @@ -47,7 +47,7 @@ public void HeapBasedArray() public void PrintLiteral(dynamic value, string expected) { SourceAsString += - @"func test_func() = + @"sub test_func() = { printLine("+value+@"); }"; @@ -61,7 +61,7 @@ public void PrintLiteral(dynamic value, string expected) public void PrintVariable(dynamic value, string expected) { SourceAsString += - @"func test_func() = + @"sub test_func() = { var x = "+value+@"; print(x); @@ -77,13 +77,13 @@ public void PrintVariable(dynamic value, string expected) public void PassVariable(dynamic value, string expected) { SourceAsString += - @"func test_func() = + @"sub test_func() = { var x = "+value+@"; varpass(x); } - func varpass(var x) = + sub varpass(var x) = { print(x); }"; @@ -98,18 +98,18 @@ func varpass(var x) = public void PrintThreeLevelsNesting(dynamic value, string expected) { SourceAsString += - @"func test_func() = + @"sub test_func() = { var y = "+value+@"; nest1(y); } - func nest1(var y) = + sub nest1(var y) = { nest2(y); } - func nest2(var z) = + sub nest2(var z) = { print(z); }"; @@ -126,7 +126,7 @@ func nest2(var z) = public void MultipleVariables(dynamic value, string expected) { SourceAsString += @" - func test_func() = + sub test_func() = { var z = 3; var x="+value+@"; diff --git a/src/JukaUnitTest/IfWhileUnitTest.cs b/src/JukaUnitTest/IfWhileUnitTest.cs index 83c0f484..93220dad 100644 --- a/src/JukaUnitTest/IfWhileUnitTest.cs +++ b/src/JukaUnitTest/IfWhileUnitTest.cs @@ -9,7 +9,7 @@ public class IfWhileUnitTest : UnitTestStructure public void IfBoolean() { SourceAsString += - @"func test_func() = + @"sub test_func() = { var x = true; if ( x == true) @@ -29,7 +29,7 @@ public void IfBoolean() public void IfBooleanElseBranch() { SourceAsString += - @"func test_func() = + @"sub test_func() = { var x = false; if ( x == true) @@ -49,7 +49,7 @@ public void IfBooleanElseBranch() public void WhileBoolean() { SourceAsString += - @"func test_func() = + @"sub test_func() = { var x = true; while(x == true) @@ -71,7 +71,7 @@ public void WhileBoolean() public void ForLoop(dynamic loops, string expected) { SourceAsString += @" - func test_func() = + sub test_func() = { for(var i = 0; i<" + loops + @"; i++;) { diff --git a/src/JukaUnitTest/StructureUnitTest.cs b/src/JukaUnitTest/StructureUnitTest.cs index fa97a10e..422034a2 100644 --- a/src/JukaUnitTest/StructureUnitTest.cs +++ b/src/JukaUnitTest/StructureUnitTest.cs @@ -11,18 +11,18 @@ public void Class() SourceAsString += @" class x = { - func xmethod() = + sub xmethod() = { print(""foo""); } - func zmethod() = + sub zmethod() = { print(""bar""); } } - func test_func() = + sub test_func() = { var v = x(); v.xmethod(); diff --git a/src/JukaUnitTest/SystemCallsUnitTest.cs b/src/JukaUnitTest/SystemCallsUnitTest.cs index d0403c68..a2551666 100644 --- a/src/JukaUnitTest/SystemCallsUnitTest.cs +++ b/src/JukaUnitTest/SystemCallsUnitTest.cs @@ -10,12 +10,12 @@ public class SystemCallsUnitTest : UnitTestStructure public void Primitives(string primitive) { SourceAsString += @" - func test_func() = + sub test_func() = { testme(); } - func testme() = + sub testme() = { var v = " + primitive + @"; print(v); diff --git a/src/JukaUnitTest/UnitTestStructure.cs b/src/JukaUnitTest/UnitTestStructure.cs index 8cc6fdde..3d0c52d6 100644 --- a/src/JukaUnitTest/UnitTestStructure.cs +++ b/src/JukaUnitTest/UnitTestStructure.cs @@ -9,7 +9,7 @@ public abstract class UnitTestStructure /// The source code string to be tested. The default main function calls a test function. /// public string SourceAsString { get; set; } = - @"func main() = + @"sub main() = { test_func(); }";