253 lines
9.1 KiB
C#
253 lines
9.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using SDL2;
|
|
|
|
namespace Core
|
|
{
|
|
public class Operations
|
|
{
|
|
public Dictionary<string, int> Variables = new Dictionary<string, int>();
|
|
public static int WindowSize;
|
|
public int Accumulator = 0;
|
|
public int StackX = 0;
|
|
public int StackY = 0;
|
|
public int[] Memory = new int[256];
|
|
public string Filename;
|
|
public int pointer = 0;
|
|
|
|
// Declare them here without `new int[WindowSize]`
|
|
public int[,] PixBufferR;
|
|
public int[,] PixBufferG;
|
|
public int[,] PixBufferB;
|
|
public int[] ColorBuffer = new int[3];
|
|
|
|
public int f_IsEqu;
|
|
public int f_IsZer;
|
|
|
|
public Operations(int pixBufSize)
|
|
{
|
|
WindowSize = pixBufSize;
|
|
}
|
|
|
|
|
|
private int ResolveValue(string shite)
|
|
{
|
|
if (shite.Any(char.IsLetter))
|
|
{
|
|
switch (shite)
|
|
{
|
|
case "ACC":
|
|
return Accumulator;
|
|
case "RAX":
|
|
return StackX;
|
|
case "RAY":
|
|
return StackY;
|
|
case "f_IsEqual":
|
|
return f_IsEqu;
|
|
case "f_IsZero":
|
|
return f_IsZer;
|
|
}
|
|
Variables.TryGetValue(shite, out int a);
|
|
return a;
|
|
}
|
|
else
|
|
{
|
|
int.TryParse(shite, out int a);
|
|
return a;
|
|
}
|
|
}
|
|
|
|
private int booltoint(bool val)
|
|
{
|
|
return (val == true) ? 1 : 0;
|
|
}
|
|
|
|
|
|
public void ExecuteCommand(ComponentType type, string[] args)
|
|
{
|
|
switch (type)
|
|
{
|
|
default: return;
|
|
// Variables
|
|
case ComponentType.NVAR:
|
|
string name = args[1];
|
|
int value = ResolveValue(args[2]);
|
|
Variables.Add(name, value);
|
|
return;
|
|
case ComponentType.RVAR:
|
|
string rname = args[1];
|
|
Variables.Remove(rname);
|
|
return;
|
|
case ComponentType.SVAR:
|
|
string sname = args[1];
|
|
int svalue = ResolveValue(args[2]);
|
|
Variables[sname] = svalue;
|
|
return;
|
|
|
|
|
|
// Memory/stack shite
|
|
case ComponentType.LDA:
|
|
Accumulator = Memory[ResolveValue(args[1])];
|
|
return;
|
|
case ComponentType.STA:
|
|
Memory[ResolveValue(args[1])] = Accumulator;
|
|
return;
|
|
case ComponentType.LDX:
|
|
StackX = Accumulator;
|
|
return;
|
|
case ComponentType.LDY:
|
|
StackY = Accumulator;
|
|
return;
|
|
case ComponentType.STX:
|
|
Accumulator = StackX;
|
|
return;
|
|
case ComponentType.STY:
|
|
Accumulator = StackY;
|
|
return;
|
|
case ComponentType.SEA:
|
|
Accumulator = ResolveValue(args[1]);
|
|
return;
|
|
|
|
|
|
// flow control
|
|
case ComponentType.JMP:
|
|
pointer = ResolveValue(args[1]) - 1;
|
|
return;
|
|
case ComponentType.JNZE:
|
|
if (f_IsZer != 1)
|
|
{
|
|
pointer = ResolveValue(args[1]) - 1;
|
|
}
|
|
return;
|
|
case ComponentType.JIE:
|
|
if (f_IsEqu== 1)
|
|
{
|
|
pointer = ResolveValue(args[1]) - 1;
|
|
}
|
|
return;
|
|
case ComponentType.GIE:
|
|
f_IsEqu = booltoint(StackX == StackY);
|
|
return;
|
|
|
|
// math
|
|
case ComponentType.ADD:
|
|
Accumulator = StackX + StackY;
|
|
return;
|
|
case ComponentType.SUB:
|
|
Accumulator = StackX - StackY;
|
|
return;
|
|
case ComponentType.MUL:
|
|
Accumulator = StackX * StackY;
|
|
return;
|
|
case ComponentType.DIV:
|
|
Accumulator = StackX / StackY;
|
|
return;
|
|
|
|
// drawing
|
|
case ComponentType.CPXBF:
|
|
ExecuteCommand(ComponentType.SVAR, new[] { args[1], WindowSize.ToString() });
|
|
PixBufferR = new int[WindowSize, WindowSize];
|
|
PixBufferG = new int[WindowSize, WindowSize];
|
|
PixBufferB = new int[WindowSize, WindowSize];
|
|
return;
|
|
case ComponentType.DCLR:
|
|
ColorBuffer = new int[] { args[1] };
|
|
|
|
|
|
|
|
// Debug commands
|
|
case ComponentType.PRA:
|
|
Console.WriteLine(Accumulator);
|
|
return;
|
|
case ComponentType.PRX:
|
|
Console.WriteLine(StackX);
|
|
return;
|
|
case ComponentType.PRY:
|
|
Console.WriteLine(StackY);
|
|
return;
|
|
case ComponentType.PRM:
|
|
int.TryParse(args[1], out int valB);
|
|
Console.WriteLine(Memory[valB]);
|
|
return;
|
|
case ComponentType.HLT:
|
|
while (true) { }
|
|
}
|
|
}
|
|
public void EquParserLoop(string equ)
|
|
{
|
|
string[] lines = equ.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
|
|
|
|
// Pass 1: Collect labels with correct line tracking
|
|
for (int i = 0; i < lines.Length; i++)
|
|
{
|
|
string trimmedLine = lines[i].Trim();
|
|
if (trimmedLine.StartsWith("@"))
|
|
{
|
|
string labelName = trimmedLine.Substring(1);
|
|
Variables[labelName] = i; // Point to the line index
|
|
}
|
|
}
|
|
|
|
// Pass 2: Execution loop
|
|
while (pointer < lines.Length)
|
|
{
|
|
string[] tokens = lines[pointer].Split(new char[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
if (tokens.Length == 0 || tokens[0].StartsWith("@"))
|
|
{
|
|
pointer++;
|
|
continue;
|
|
}
|
|
|
|
string command = tokens[0];
|
|
|
|
switch (command)
|
|
{
|
|
case "::":
|
|
break;
|
|
// Variables
|
|
case "NVAR": ExecuteCommand(ComponentType.NVAR, tokens); break;
|
|
case "RVAR": ExecuteCommand(ComponentType.RVAR, tokens); break;
|
|
case "SVAR": ExecuteCommand(ComponentType.SVAR, tokens); break;
|
|
|
|
// Memory
|
|
case "LDA": ExecuteCommand(ComponentType.LDA, tokens); break;
|
|
case "STA": ExecuteCommand(ComponentType.STA, tokens); break;
|
|
case "LDX": ExecuteCommand(ComponentType.LDX, tokens); break;
|
|
case "LDY": ExecuteCommand(ComponentType.LDY, tokens); break;
|
|
case "STX": ExecuteCommand(ComponentType.STX, tokens); break;
|
|
case "STY": ExecuteCommand(ComponentType.STY, tokens); break;
|
|
case "SEA": ExecuteCommand(ComponentType.SEA, tokens); break;
|
|
|
|
// Flow control
|
|
case "JMP": ExecuteCommand(ComponentType.JMP, tokens); break;
|
|
case "JNZE": ExecuteCommand(ComponentType.JNZE, tokens); break;
|
|
case "JIE": ExecuteCommand(ComponentType.JIE, tokens); break;
|
|
case "GIE": ExecuteCommand(ComponentType.GIE, tokens); break;
|
|
|
|
// Math (Added missing operations)
|
|
case "ADD": ExecuteCommand(ComponentType.ADD, tokens); break;
|
|
case "SUB": ExecuteCommand(ComponentType.SUB, tokens); break;
|
|
case "MUL": ExecuteCommand(ComponentType.MUL, tokens); break;
|
|
case "DIV": ExecuteCommand(ComponentType.DIV, tokens); break;
|
|
|
|
// drawing
|
|
case "CPXBF": break; // create pixel buffer
|
|
case "PBSET": break; // set a pixel in the buffer
|
|
case "PBCLEAR": break; // clear buffer
|
|
case "DCLR": break; // set draw color
|
|
|
|
// Debug
|
|
case "PRA": ExecuteCommand(ComponentType.PRA, tokens); break;
|
|
case "PRX": ExecuteCommand(ComponentType.PRX, tokens); break;
|
|
case "PRY": ExecuteCommand(ComponentType.PRY, tokens); break;
|
|
case "PRM": ExecuteCommand(ComponentType.PRM, tokens); break;
|
|
case "HLT": ExecuteCommand(ComponentType.HLT, tokens); break;
|
|
}
|
|
pointer++;
|
|
}
|
|
}
|
|
}
|
|
} |