Skip to content

Commit

Permalink
Can now declare variables without "var" e.g. x=3;
Browse files Browse the repository at this point in the history
  • Loading branch information
TheAndreiM committed Jul 15, 2024
1 parent f0b3c21 commit cbf3619
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 67 deletions.
12 changes: 6 additions & 6 deletions src/Juka/REPL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private static async Task InitializeRepl()


// Create the layout
var layout = new Layout("Root")
Layout layout = new Layout("Root")
.SplitColumns(
new Layout("Left").SplitRows(
new Layout("Logo"),
Expand All @@ -74,8 +74,8 @@ private static async Task InitializeRepl()


layout["Left"].Ratio(2);
var archicture = SelfUpdate.GetSystemInfo();

Dictionary<string, string> archicture = SelfUpdate.GetSystemInfo();



Expand All @@ -90,8 +90,8 @@ private static async Task InitializeRepl()
)));


var logotext = new FigletText("Juka").Color(Color.Purple);
var logopanel = new Panel(logotext).Expand();
FigletText logotext = new FigletText("Juka").Color(Color.Purple);
Panel logopanel = new Panel(logotext).Expand();
logopanel.Padding = new Padding(3, 3, 3, 3);
logopanel.Expand = true;
logopanel.Border = BoxBorder.None;
Expand Down Expand Up @@ -134,7 +134,7 @@ private static async Task HandleCommandAsync(string command)
AnsiConsole.Write(DisplayMenuTable());
break;
case "!!info":
var archtecture = SelfUpdate.GetSystemInfo();
Dictionary<string, string> archtecture = SelfUpdate.GetSystemInfo();
// Display system information
AnsiConsole.MarkupLine($"[bold blue]Your Operating System:[/] [green]"+archtecture["platform"]+"[/]");
AnsiConsole.MarkupLine($"[bold blue]Current Directory:[/] [green]"+archtecture["dir"]+"[/]");
Expand Down
8 changes: 0 additions & 8 deletions src/JukaCompiler/Compiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,6 @@ public string CompileJukaCode(string data, bool isFile = true)
Parser parser = new(new Scanner(data, _serviceProvider, isFile), _serviceProvider);
List<Statement> statements = parser.Parse();


/*foreach (var array in statements)
{
Console.WriteLine(array);
}*/

return Compile(statements);
}
catch (Exception ex)
Expand Down
2 changes: 1 addition & 1 deletion src/JukaCompiler/Expressions/Expr.cs
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ internal override R Accept<R>(IVisitor<R> visitor)

internal object? LiteralValue()
{
if (value == null)
if (this.value == null)
{
throw new ArgumentNullException("literal");
}
Expand Down
58 changes: 26 additions & 32 deletions src/JukaCompiler/Interpreter/JukaInterpreter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,11 @@ private Statement.Print VisitPrintAllInternal(Expr expr, Action<object> printAct

if(expr is Expr.Binary)
{
var binaryEval = Evaluate(expr);
object? binaryEval = Evaluate(expr);

if (binaryEval is Expr.LexemeTypeLiteral lexemeTypeLiteral)
{
var literal = lexemeTypeLiteral.literal;
object? literal = lexemeTypeLiteral.literal;
if (literal != null)
printAction(literal);
}
Expand All @@ -214,7 +214,7 @@ private Statement.Print VisitPrintAllInternal(Expr expr, Action<object> printAct
{
if (variableExpression != null)
{
var arrayData = stackVariableState?.arrayValues[variableExpression.ArrayIndex];
object? arrayData = stackVariableState?.arrayValues[variableExpression.ArrayIndex];
if (arrayData is Expr.Literal)
{
PrintLiteral((Expr.Literal)arrayData, printAction);
Expand Down Expand Up @@ -247,7 +247,7 @@ private bool PrintVariableLookUp(Expr.Variable expr, Action<object> printTypeAct
{
if (expr.ExpressionLexeme != null)
{
var variable = LookUpVariable(expr.ExpressionLexeme, expr);
object? variable = LookUpVariable(expr.ExpressionLexeme, expr);

if (variable is StackVariableState stackVariable)
{
Expand Down Expand Up @@ -311,7 +311,7 @@ public Statement VisitForStmt(Statement.For stmt)
{
stmt.Init.Accept(this);

var breakExpression = Evaluate(stmt.BreakExpr);
object? breakExpression = Evaluate(stmt.BreakExpr);
while (IsTrue(breakExpression))
{
Execute(stmt.ForBody);
Expand Down Expand Up @@ -369,21 +369,21 @@ public object VisitLexemeTypeLiteral(Expr.LexemeTypeLiteral expr)
public object VisitAssignExpr(Expr.Assign expr)
{
object? value = Evaluate(expr.value);
var stackVariableState = new StackVariableState
object? stackVariableState = new StackVariableState
{
Name = expr.ExpressionLexeme?.ToString()!,
Value = value,
type = expr.GetType(),
expressionContext = expr,
};

var stackFrame = frames.Peek();
StackFrame stackFrame = frames.Peek();

if (!stackFrame.UpdateVariable(expr.ExpressionLexeme?.ToString() ?? string.Empty, stackVariableState))
{
if (expr.ExpressionLexeme != null)
{
var newVar = new Statement.Var(expr.ExpressionLexeme, expr);
Statement.Var newVar = new Statement.Var(expr.ExpressionLexeme, expr);
stackFrame.AddVariable(newVar, this);
}
}
Expand All @@ -396,7 +396,7 @@ public object VisitAssignExpr(Expr.Assign expr)
{
if (expr is Variable)
{
var stackVariableState = Evaluate(expr) as StackVariableState;
StackVariableState? stackVariableState = Evaluate(expr) as StackVariableState;

if (stackVariableState?.expressionContext is Literal context)
{
Expand All @@ -422,13 +422,7 @@ public object VisitAssignExpr(Expr.Assign expr)
if (expr is Expr.Binary binary)
{
Expr.LexemeTypeLiteral visited = (Expr.LexemeTypeLiteral)VisitBinaryExpr(binary);


var x = 1;
return (visited.literal.ToString(), visited.LexemeType);



}
if (expr is Literal expr1) return (expr1.ExpressionLexeme?.ToString(), expr1.Type);
throw new JRuntimeException("Can't get literal data" + expr);
Expand Down Expand Up @@ -469,7 +463,7 @@ private static object IsLessThan(LexemeType.Types leftValueType, LexemeType.Type
{
if (leftValueType == LexemeType.Types.NUMBER && rightValueType == LexemeType.Types.NUMBER)
{
var literal = new Expr.LexemeTypeLiteral
LexemeTypeLiteral literal = new Expr.LexemeTypeLiteral
{
literal = Convert.ToDouble(leftValue) < Convert.ToDouble(rightValue),
lexemeType = LexemeType.Types.BOOL
Expand All @@ -489,7 +483,7 @@ private static object IsGreaterThan(LexemeType.Types leftValueType, LexemeType.T
{
if (leftValueType == LexemeType.Types.NUMBER && rightValueType == LexemeType.Types.NUMBER)
{
var literal = new Expr.LexemeTypeLiteral
LexemeTypeLiteral literal = new Expr.LexemeTypeLiteral
{
literal = Convert.ToDouble(leftValue) > Convert.ToDouble(rightValue),
lexemeType = LexemeType.Types.BOOL
Expand All @@ -509,7 +503,7 @@ private static object AddTypes(LexemeType.Types leftValueType, LexemeType.Types
{
if (leftValueType == LexemeType.Types.NUMBER && rightValueType == LexemeType.Types.NUMBER)
{
var literalSum = new Expr.LexemeTypeLiteral
LexemeTypeLiteral literalSum = new Expr.LexemeTypeLiteral
{
literal = Convert.ToDouble(leftValue) + Convert.ToDouble(rightValue),
lexemeType = LexemeType.Types.NUMBER
Expand All @@ -520,7 +514,7 @@ private static object AddTypes(LexemeType.Types leftValueType, LexemeType.Types

if ((leftValueType == LexemeType.Types.NUMBER && rightValueType == LexemeType.Types.STRING) || (leftValueType == LexemeType.Types.STRING && rightValueType == LexemeType.Types.NUMBER))
{
var literalStringSum = new Expr.LexemeTypeLiteral
LexemeTypeLiteral literalStringSum = new Expr.LexemeTypeLiteral
{
literal = Convert.ToString(leftValue) + Convert.ToString(rightValue),
lexemeType = LexemeType.Types.STRING
Expand All @@ -531,7 +525,7 @@ private static object AddTypes(LexemeType.Types leftValueType, LexemeType.Types

if (leftValueType == LexemeType.Types.STRING && rightValueType == LexemeType.Types.STRING)
{
var literalStringSum = new Expr.LexemeTypeLiteral
LexemeTypeLiteral literalStringSum = new Expr.LexemeTypeLiteral
{
literal = Convert.ToString(leftValue) + Convert.ToString(rightValue),
lexemeType = LexemeType.Types.STRING
Expand All @@ -546,7 +540,7 @@ private static object SubtractTypes(LexemeType.Types leftValueType, LexemeType.T
{
if (leftValueType == LexemeType.Types.NUMBER && rightValueType == LexemeType.Types.NUMBER)
{
var literalSum = new Expr.LexemeTypeLiteral
LexemeTypeLiteral literalSum = new Expr.LexemeTypeLiteral
{
literal = Convert.ToDouble(leftValue) - Convert.ToDouble(rightValue),
lexemeType = LexemeType.Types.NUMBER
Expand All @@ -566,7 +560,7 @@ private static object MultiplyTypes(LexemeType.Types leftValueType, LexemeType.T
{
if (leftValueType == LexemeType.Types.NUMBER && rightValueType == LexemeType.Types.NUMBER)
{
var literalProduction = new Expr.LexemeTypeLiteral
LexemeTypeLiteral literalProduction = new Expr.LexemeTypeLiteral
{
literal = Convert.ToDouble(leftValue) * Convert.ToDouble(rightValue),
lexemeType = LexemeType.Types.NUMBER
Expand All @@ -585,7 +579,7 @@ private static object DivideTypes(LexemeType.Types leftValueType, LexemeType.Typ
{
if (leftValueType == LexemeType.Types.NUMBER && rightValueType == LexemeType.Types.NUMBER)
{
var literalProduction = new Expr.LexemeTypeLiteral();
LexemeTypeLiteral literalProduction = new Expr.LexemeTypeLiteral();
double divident = Convert.ToDouble(rightValue);

if (divident == 0)
Expand Down Expand Up @@ -763,16 +757,16 @@ public object VisitUnaryExpr(Expr.Unary expr)

if (stackVariableState?.expressionContext is Literal context && ((Unary)expr).LexemeType == LexemeType.Types.PLUSPLUS)
{
var unaryUpdate = Convert.ToInt64(context.ExpressionLexeme?.ToString()) + 1;
long unaryUpdate = Convert.ToInt64(context.ExpressionLexeme?.ToString()) + 1;

var lexeme = new Lexeme(LexemeType.Types.NUMBER, 0, 0);
Lexeme lexeme = new Lexeme(LexemeType.Types.NUMBER, 0, 0);
lexeme.AddToken(unaryUpdate.ToString());

var newLiteral = new Literal(lexeme, lexeme.LexemeType);
Literal newLiteral = new Literal(lexeme, lexeme.LexemeType);

stackVariableState.expressionContext = newLiteral;

var lexemeTypeLiteral = new LexemeTypeLiteral
LexemeTypeLiteral lexemeTypeLiteral = new LexemeTypeLiteral
{
LexemeType = lexeme.LexemeType,
ExpressionLexeme = lexeme,
Expand All @@ -791,7 +785,7 @@ public object VisitVariableExpr(Expr.Variable expr)
{
if (expr.ExpressionLexeme != null)
{
var lookUp = LookUpVariable(expr.ExpressionLexeme, expr);
object? lookUp = LookUpVariable(expr.ExpressionLexeme, expr);
if (lookUp == null)
{
throw new("Variable is null");
Expand All @@ -805,7 +799,7 @@ public object VisitVariableExpr(Expr.Variable expr)

public object VisitArrayExpr(Expr.ArrayDeclarationExpr expr)
{
var currentFrame = frames.Peek();
object? currentFrame = frames.Peek();
//if (expr.initializerContextVariableName != null)
// currentFrame.AddStackArray(expr.initializerContextVariableName, expr.ArrayIndex);
return expr.ArraySize;
Expand All @@ -819,7 +813,7 @@ public object VisitNewExpr(NewDeclarationExpr expr)

public object VisitArrayAccessExpr(ArrayAccessExpr expr)
{
var stackVariableState = LookUpVariable(expr.ArrayVariableName, expr) as StackVariableState;
StackVariableState? stackVariableState = LookUpVariable(expr.ArrayVariableName, expr) as StackVariableState;

int arraySize = 0;
if (stackVariableState?.expressionContext is ArrayDeclarationExpr arrayContext)
Expand Down Expand Up @@ -849,7 +843,7 @@ public object VisitArrayAccessExpr(ArrayAccessExpr expr)
public object VisitDeleteExpr(DeleteDeclarationExpr expr)
{

var currentFrame = frames.Peek();
StackFrame currentFrame = frames.Peek();
currentFrame.DeleteVariable(expr.variable.ExpressionLexemeName);

//Console.WriteLine(currentFrame);
Expand Down Expand Up @@ -898,7 +892,7 @@ private bool IsTrue(object? o)
return b;
case LexemeTypeLiteral literal:
{
var boolValue = literal.Literal;
object boolValue = literal.Literal;
if (boolValue is bool)
{
return IsTrue(boolValue);
Expand Down
7 changes: 4 additions & 3 deletions src/JukaCompiler/Interpreter/Resolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private void Resolve(Expr expr)

public object VisitAssignExpr(Expr.Assign expr)
{
var frame = blockScope.Peek() ?? throw new Exception(string.Format(errorMessage, "No stack frames"));
string frame = blockScope.Peek() ?? throw new Exception(string.Format(errorMessage, "No stack frames"));
if (processScope.ContainsKey(frame))
{
BlockScope? value = processScope[frame];
Expand All @@ -80,7 +80,7 @@ public object VisitAssignExpr(Expr.Assign expr)
string lexemeKey = expr.ExpressionLexeme?.ToString()!;
if (value.lexemeScope.ContainsKey(lexemeKey))
{
var lexeme = value.lexemeScope[lexemeKey];
Lexeme lexeme = value.lexemeScope[lexemeKey];
//value.lexemeScope[expr.ExpressionLexeme.ToString] = expr.
//var xx= interpreter.LookUpVariable(lexeme, expr);
}
Expand Down Expand Up @@ -236,6 +236,7 @@ public object VisitIfStmt(Statement.If stmt)

public object VisitLiteralExpr(Expr.Literal expr)
{

return new Expr.Literal();
}

Expand Down Expand Up @@ -401,7 +402,7 @@ private void Declare(Lexeme name)
{
if (blockScope != null && blockScope.Count > 0)
{
var block = blockScope.Peek();
string block = blockScope.Peek();
if (block.Equals(name.ToString()))
{
// current context is a function
Expand Down
2 changes: 1 addition & 1 deletion src/JukaCompiler/Interpreter/StackFrame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ internal void AddVariables(Dictionary<string, object?> variables, JukaInterprete

internal void AddVariable(string name, object? variableValue, Type? variableKind, Expr? expressionContext)
{
var stackVariableState = new StackVariableState
StackVariableState stackVariableState = new StackVariableState
{
Name = name,
Value = variableValue,
Expand Down
Loading

0 comments on commit cbf3619

Please sign in to comment.