Skip to content

Commit

Permalink
"sub" command added and updated
Browse files Browse the repository at this point in the history
  • Loading branch information
TheAndreiM committed Jul 7, 2024
1 parent b0d1e89 commit 26e942b
Show file tree
Hide file tree
Showing 32 changed files with 263 additions and 290 deletions.
2 changes: 1 addition & 1 deletion examples/HelloWorld.juk
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
func main() = {
sub main() = {
// Print Hello World
printLine("Hello World");
}
Expand Down
4 changes: 2 additions & 2 deletions examples/classtest.juk
Original file line number Diff line number Diff line change
@@ -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());
}
Expand Down
10 changes: 5 additions & 5 deletions examples/dontest.juk
Original file line number Diff line number Diff line change
@@ -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() = {
}
}
6 changes: 3 additions & 3 deletions examples/scanner.juk
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
func main()={
sub main()={
if ( 2<3 )
{
printLine("foo");
Expand All @@ -9,7 +9,7 @@ func main()={
}
}

func foo() = {
sub foo() = {
var x = 3;
while (x<=4)
{
Expand All @@ -18,7 +18,7 @@ func foo() = {
}
}

func three()={
sub three()={
3+3
x=function(3+3);
x=1++==4
Expand Down
4 changes: 2 additions & 2 deletions examples/test.juk
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
func process() =
sub process() =
{
printLine("process");
}


func main() =
sub main() =
{
printLine("main");
process();
Expand Down
4 changes: 2 additions & 2 deletions examples/test2.juk
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
func testC_sharp() = {
sub testC_sharp() = {
printLine("AsdfA");
}

func main() = {
sub main() = {
testC_sharp();
}
76 changes: 46 additions & 30 deletions src/Juka/REPL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ namespace Juka
public class Repl
{
private static Compiler? compiler;
private static Stack<string> funcData = new();
private static Stack<string> subData = new();
private static readonly Stack<string> redoStack = new();
private static string? dataStart;
private static string? dataEnd;
private static bool isFuncOrClass;
private static bool isSubOrClass;

public static async Task RunRepl()
{
Expand Down Expand Up @@ -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<string>();
subData = new Stack<string>();

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");
Expand Down Expand Up @@ -90,6 +91,9 @@ private static async Task HandleCommandAsync(string command)
case "!undo":
UndoLastCommand();
break;
case "!redo":
RedoLastCommand();
break;
case "!update":
await UpdateJuka();
break;
Expand All @@ -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
Expand All @@ -136,7 +140,7 @@ private static void HandleCode(string code)
code = "";
}

funcData.Push(code);
subData.Push(code);
ExecuteLine();
}
}
Expand All @@ -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();
}
Expand All @@ -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);
}
Expand All @@ -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();
Expand All @@ -213,23 +229,23 @@ 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<string>();
subData = new Stack<string>();

dataEnd += userDataToExecute.ToString();
DisplayPrompt();
}

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";
Expand Down
4 changes: 2 additions & 2 deletions src/JukaApi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IResult> ExecuteCodeFromBody(HttpRequest request)
Expand Down
12 changes: 3 additions & 9 deletions src/JukaAzureFunction/JukaAzureFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,12 @@

namespace JukaAzureFunction;

public class JukaAzureFunction
public class JukaAzureFunction(ILogger<JukaAzureFunction> log)
{
private readonly ILogger<JukaAzureFunction> logger;
private readonly ILogger<JukaAzureFunction> logger = log;

public JukaAzureFunction(ILogger<JukaAzureFunction> 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")]
Expand Down
45 changes: 22 additions & 23 deletions src/JukaCompiler/Compiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ICompilerError, CompilerError>();
Expand All @@ -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
{
Expand Down Expand Up @@ -96,25 +85,35 @@ private static void SetupMainMethodRuntimeHook(List<Stmt> statements, Resolver r
var mainFunction = statements.OfType<Stmt.Function>().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<ICompilerError>().HasErrors();
CheckServiceProvider();
return serviceProvider.GetRequiredService<ICompilerError>().HasErrors();
}

public List<string> ListErrors()
{
CheckServiceProvider();
return serviceProvider.GetRequiredService<ICompilerError>().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<ICompilerError>().ListErrors();
}
}
public class CompilerException(string message, Exception innerException) : Exception(message, innerException)
{
}
}


Loading

0 comments on commit 26e942b

Please sign in to comment.