Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
TheAndreiM committed Jul 8, 2024
1 parent 01aecf6 commit 6bdaa4d
Show file tree
Hide file tree
Showing 21 changed files with 122 additions and 114 deletions.
4 changes: 2 additions & 2 deletions .cirrus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ task:
matrix:
- name: FreeBSD Build
freebsd_instance:
image_family: freebsd-14-0
image_family: freebsd-13-3
environment:
- GITHUB_TOKEN: ENCRYPTED[!b7770b286394d89374afd6ce2ef353693b8b922c6c0d6169c4e55fb926ea26507660d0759f6a2cdf2e2436e514742573!]
- JUKA_TOKEN: Unix_X64
Expand Down Expand Up @@ -34,7 +34,7 @@ task:
- JUKA_TOKEN: Linux_X86
setup_script:
- dpkg --add-architecture i386
- sudo add-apt-repository universe
- add-apt-repository universe
- apt-get update
- apt-get install -y wget curl libc6:i386 libncurses5:i386 libstdc++6:i386 libicu-dev:i386 libssl-dev:i386 zlib1g:i386
- mkdir /usr/share/dotnet
Expand Down
16 changes: 8 additions & 8 deletions src/Juka/REPL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class Repl
{
private static Compiler? compiler;
private static Stack<string> subData = new();
private static readonly Stack<string> redoStack = new();
private static Stack<string> redoStack = new();
private static string? dataStart;
private static string? dataEnd;
private static bool isSubOrClass;
Expand Down Expand Up @@ -134,11 +134,11 @@ private static void HandleCode(string code)
}
else
{
if (code.StartsWith("var"))
{
//if (code.StartsWith("var"))
//{
dataStart += code;
code = "";
}
//}

subData.Push(code);
ExecuteLine();
Expand All @@ -147,7 +147,7 @@ private static void HandleCode(string code)

private static void DisplayMenu()
{
var table = new Table();
Table table = new();

// Add some columns
table.AddColumn("Command");
Expand Down Expand Up @@ -181,7 +181,7 @@ private static void ClearConsole()

private static void ListCode()
{
foreach (var data in subData.Reverse())
foreach (string? data in subData.Reverse())
{
Console.WriteLine(data);
}
Expand All @@ -197,15 +197,15 @@ private static void GetLibraries()

private static void UndoLastCommand()
{
var templine = subData.Pop();
string templine = subData.Pop();
redoStack.Push(templine);
AnsiConsole.MarkupLine("[bold red]Removed: [/]" + templine);
DisplayPrompt();
}

private static void RedoLastCommand()
{
var templine = redoStack.Pop();
string templine = redoStack.Pop();
subData.Push(templine);
AnsiConsole.MarkupLine("[bold green]Added: [/]" + templine);
DisplayPrompt();
Expand Down
2 changes: 1 addition & 1 deletion src/Juka/SelfUpdate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Juka;

class SelfUpdate
{
private static readonly string? CurrentVersion = Juka.CurrentVersion.Get();
private static string? CurrentVersion = Juka.CurrentVersion.Get();
public static async Task<string> Check()
{
if (CurrentVersion == "DEBUG")
Expand Down
50 changes: 25 additions & 25 deletions src/Juka/TerminalJuka.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,61 +13,61 @@ public static async Task Perform(string[] args)
switch (userInput)
{
case "--encrypt":
var sourceFilename = args[1];
var destinationFilename = args[1]+".encrypt";
string sourceFilename = args[1];
string destinationFilename = args[1]+".encrypt";

AnsiConsole.MarkupLine($"[bold yellow]Encrypting File: " + sourceFilename + " to "+ destinationFilename +" [/]");

await using (var sourceStream = File.OpenRead(sourceFilename))
await using (var destinationStream = File.Create(destinationFilename))
using (var provider = Aes.Create())
using (var cryptoTransform = provider.CreateEncryptor())
await using (var cryptoStream = new CryptoStream(destinationStream, cryptoTransform, CryptoStreamMode.Write))
await using (FileStream sourceStream = File.OpenRead(sourceFilename))
await using (FileStream destinationStream = File.Create(destinationFilename))
using (Aes provider = Aes.Create())
using (ICryptoTransform cryptoTransform = provider.CreateEncryptor())
await using (CryptoStream cryptoStream = new CryptoStream(destinationStream, cryptoTransform, CryptoStreamMode.Write))
{
destinationStream.Write(provider.IV, 0, provider.IV.Length);
await sourceStream.CopyToAsync(cryptoStream);
var mykey = Convert.ToBase64String(provider.Key);
string mykey = Convert.ToBase64String(provider.Key);
await File.WriteAllTextAsync(sourceFilename + ".key", mykey);
Console.WriteLine(mykey);
}
break;
case "--encrypted":
var encFile = args[1] + ".encrypt";
var encKeyfile = args[1] + ".key";
string encFile = args[1] + ".encrypt";
string encKeyfile = args[1] + ".key";

var keyEncrypted = Convert.FromBase64String(await File.ReadAllTextAsync(encKeyfile));
byte[] keyEncrypted = Convert.FromBase64String(await File.ReadAllTextAsync(encKeyfile));

string? plainText;

await using (var sourceStream = File.OpenRead(encFile))
using (var provider = Aes.Create())
await using (FileStream sourceStream = File.OpenRead(encFile))
using (Aes provider = Aes.Create())
{
var IV = new byte[provider.IV.Length];
byte[] IV = new byte[provider.IV.Length];
sourceStream.Read(IV, 0, IV.Length);
using var cryptoTransform = provider.CreateDecryptor(keyEncrypted, IV);
await using var cryptoStream = new CryptoStream(sourceStream, cryptoTransform, CryptoStreamMode.Read);
using ICryptoTransform cryptoTransform = provider.CreateDecryptor(keyEncrypted, IV);
await using CryptoStream cryptoStream = new CryptoStream(sourceStream, cryptoTransform, CryptoStreamMode.Read);
using StreamReader reader = new(cryptoStream);
plainText = await reader.ReadToEndAsync();
}
DebugMe.DebugMode = 0;
Console.WriteLine(new Compiler().Go(plainText, isFile: false));
break;
case "--decrypt":
var encryptedFile = args[1]+".encrypt";
var unencryptedFile = args[1];
var keyfile = args[1] + ".key";
string encryptedFile = args[1]+".encrypt";
string unencryptedFile = args[1];
string keyfile = args[1] + ".key";

var key = Convert.FromBase64String(await File.ReadAllTextAsync(keyfile));
byte[] key = Convert.FromBase64String(await File.ReadAllTextAsync(keyfile));

AnsiConsole.MarkupLine($"[bold yellow]Decrypting File: " + encryptedFile + " with key: " + keyfile + "[/]");
await using (var sourceStream = File.OpenRead(encryptedFile))
await using (var destinationStream = File.Create(unencryptedFile))
using (var provider = Aes.Create())
await using (FileStream sourceStream = File.OpenRead(encryptedFile))
await using (FileStream destinationStream = File.Create(unencryptedFile))
using (Aes provider = Aes.Create())
{
var IV = new byte[provider.IV.Length];
byte[] IV = new byte[provider.IV.Length];
sourceStream.Read(IV, 0, IV.Length);
using ICryptoTransform cryptoTransform = provider.CreateDecryptor(key, IV);
await using var cryptoStream = new CryptoStream(sourceStream, cryptoTransform, CryptoStreamMode.Read);
await using CryptoStream cryptoStream = new CryptoStream(sourceStream, cryptoTransform, CryptoStreamMode.Read);
await cryptoStream.CopyToAsync(destinationStream);
}
break;
Expand Down
24 changes: 15 additions & 9 deletions src/JukaAzureFunction/JukaAzureFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@

namespace JukaAzureFunction;

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

public JukaAzureFunction(ILogger<JukaAzureFunction> log)
{
logger = log;
}


[Function("JukaAzureFunction")]
[OpenApiOperation(operationId: "Run", tags: "name" )]
[OpenApiOperation(operationId: "Run", tags: new[] { "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 All @@ -24,8 +30,8 @@ public HttpResponseData Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "p
{
logger.LogInformation("JukaAzureFunction trigger is running.");

var query = System.Web.HttpUtility.ParseQueryString(req.Url.Query);
var code = query["code"];
System.Collections.Specialized.NameValueCollection query = System.Web.HttpUtility.ParseQueryString(req.Url.Query);
string? code = query["code"];


string assemblyVersion = Assembly.GetExecutingAssembly().GetName().Version?.ToString() ?? "DEBUG";
Expand All @@ -36,9 +42,9 @@ public HttpResponseData Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "p

if (string.IsNullOrEmpty(code))
{
var response = req.CreateResponse(HttpStatusCode.OK);
HttpResponseData response = req.CreateResponse(HttpStatusCode.OK);
response.Headers.Add("Content-Type", "text/plain; charset=utf-8");
response.WriteString("Welcome to Juka Azure Function version \""+ assemblyVersion + "\"! To execute a program, send a GET or a POST request to \"/api/JukaAzureFunction/?code=func main()={}\". Go to /api/swagger/ui to see OpenAPI documentation");
response.WriteString("Welcome to Juka Azure Function version \"" + assemblyVersion + "\"! To execute a program, send a GET or a POST request to \"/api/JukaAzureFunction/?code=func main()={}\". Go to /api/swagger/ui to see OpenAPI documentation");
return response;
}

Expand All @@ -51,14 +57,14 @@ public HttpResponseData Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "p
if (compiler.HasErrors())
{

var response2 = req.CreateResponse(HttpStatusCode.OK);
HttpResponseData response2 = req.CreateResponse(HttpStatusCode.OK);
response2.Headers.Add("Content-Type", "text/plain; charset=utf-8");
var errors = string.Join(Environment.NewLine, compiler.ListErrors());
response2.WriteString(JsonConvert.SerializeObject(new { errors, original = code }));
return response2;
}

var response3 = req.CreateResponse(HttpStatusCode.OK);
HttpResponseData response3 = req.CreateResponse(HttpStatusCode.OK);
response3.Headers.Add("Content-Type", "text/plain; charset=utf-8");
response3.WriteString(JsonConvert.SerializeObject(new { output = outputValue, original = code }));
return response3;
Expand Down
2 changes: 1 addition & 1 deletion src/JukaAzureFunction/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class Program
{
public static void Main()
{
var host = new HostBuilder()
IHost host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults(worker => worker.UseNewtonsoftJson())
.Build();

Expand Down
6 changes: 3 additions & 3 deletions src/JukaCompiler/Compiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace JukaCompiler
public class Compiler
{
private ServiceProvider serviceProvider;
private readonly HostBuilder hostBuilder;
private HostBuilder hostBuilder;


public Compiler()
Expand Down Expand Up @@ -61,7 +61,7 @@ private string Compile(List<Stmt> statements)

SetupMainMethodRuntimeHook(statements, resolver);

var currentOut = Console.Out;
TextWriter currentOut = Console.Out;

try
{
Expand All @@ -82,7 +82,7 @@ private string Compile(List<Stmt> statements)

private static void SetupMainMethodRuntimeHook(List<Stmt> statements, Resolver resolver)
{
var mainFunction = statements.OfType<Stmt.Function>().FirstOrDefault(f => f.StmtLexemeName.Equals("main")) ?? throw new Exception("No main function is defined");
Stmt.Function 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 subroutineName = new(lexeme);
Expand Down
8 changes: 4 additions & 4 deletions src/JukaCompiler/Expressions/Expr.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ internal string ExpressionLexemeName
}
internal class Assign : Expr
{
internal readonly Expr value;
internal Expr value;

internal Assign(Lexeme ExpressionLexeme, Expr value)
{
Expand Down Expand Up @@ -223,7 +223,7 @@ internal override R Accept<R>(IVisitor<R> visitor)
}
internal class Unary : Expr
{
private readonly LexemeType.Types lexemeType;
private LexemeType.Types lexemeType;
internal Unary(Lexeme lex, LexemeType.Types lexemeType)
{
expressionLexeme = lex;
Expand Down Expand Up @@ -256,8 +256,8 @@ internal override R Accept<R>(IVisitor<R> visitor)
}
internal class Literal : Expr
{
private readonly object? value;
private readonly LexemeType.Types type;
private object? value;
private LexemeType.Types type;

internal Literal(Lexeme literal, LexemeType.Types type)
{
Expand Down
6 changes: 3 additions & 3 deletions src/JukaCompiler/Interpreter/JukaClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
{
internal class JukaClass : IJukaCallable
{
private readonly string name;
private readonly JukaClass superClass;
private readonly Dictionary<string, JukaFunction> methods;
private string name;
private JukaClass superClass;
private Dictionary<string, JukaFunction> methods;

internal JukaClass(string name, JukaClass superClass, Dictionary<string, JukaFunction> methods)
{
Expand Down
4 changes: 2 additions & 2 deletions src/JukaCompiler/Interpreter/JukaEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace JukaCompiler.Interpreter
internal class JukaEnvironment
{
private JukaEnvironment? enclosing;
private readonly Dictionary<string, object?> values = [];
private Dictionary<string, object?> values = [];

internal JukaEnvironment()
{
Expand Down Expand Up @@ -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: "+name);
string nameAsString = name?.ToString() ?? throw new JRuntimeException("unable to get variable name: " + name);

if (values.ContainsKey(nameAsString))
{
Expand Down
6 changes: 3 additions & 3 deletions src/JukaCompiler/Interpreter/JukaFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ namespace JukaCompiler.Interpreter
{
internal class JukaFunction : IJukaCallable
{
private readonly Stmt.Function declaration;
private readonly JukaEnvironment closure;
private readonly bool isInitializer;
private Stmt.Function declaration;
private JukaEnvironment closure;
private bool isInitializer;

internal JukaFunction(Stmt.Function declaration, JukaEnvironment closure, bool isInitializer)
{
Expand Down
4 changes: 2 additions & 2 deletions src/JukaCompiler/Interpreter/JukaInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ namespace JukaCompiler.Interpreter
{
internal class JukaInstance
{
private readonly JukaClass Class;
private readonly Dictionary<string, object> fields = [];
private JukaClass Class;
private Dictionary<string, object> fields = [];

internal JukaInstance(JukaClass Class)
{
Expand Down
16 changes: 8 additions & 8 deletions src/JukaCompiler/Interpreter/JukaInterpreter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ namespace JukaCompiler.Interpreter
{
internal class JukaInterpreter : Stmt.IVisitor<Stmt>, Expr.IVisitor<object>
{
private readonly ServiceProvider serviceProvider;
private readonly JukaEnvironment globals;
private ServiceProvider serviceProvider;
private JukaEnvironment globals;
private JukaEnvironment environment;
private readonly Dictionary<Expr, int?> locals = new();
private readonly Stack<StackFrame> frames = new();
private readonly string globalScope = "__global__scope__";
private readonly int __max_stack_depth__ = 500;
private Dictionary<Expr, int?> locals = new();
private Stack<StackFrame> frames = new();
private string globalScope = "__global__scope__";
private int __max_stack_depth__ = 500;

internal JukaInterpreter(ServiceProvider services)
{
Expand Down Expand Up @@ -189,8 +189,8 @@ private Stmt.Print VisitPrintAllInternal(Expr expr, Action<object> printAction)

if (expr is ArrayAccessExpr)
{
var variableExpression = expr as ArrayAccessExpr;
var variableName = variableExpression?.ArrayVariableName.ToString();
ArrayAccessExpr? variableExpression = expr as ArrayAccessExpr;
string? variableName = variableExpression?.ArrayVariableName.ToString();

if (variableName != null && frames.Peek().TryGetStackVariableByName(variableName, out StackVariableState? stackVariableState))
{
Expand Down
Loading

0 comments on commit 6bdaa4d

Please sign in to comment.