rewrite, its now a vm for a shitty subset of assembly
This commit is contained in:
+187
-67
@@ -2,99 +2,219 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using SDL2;
|
||||
|
||||
namespace Core
|
||||
{
|
||||
public class Operations
|
||||
{
|
||||
public Dictionary<string, float> Variables = new Dictionary<string, float>();
|
||||
public Operations(int pixBufSize) => WindowSize = pixBufSize;
|
||||
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;
|
||||
public int[] PixBufferR = new int[WindowSize];
|
||||
public int[] PixBufferG = new int[WindowSize];
|
||||
public int[] PixBufferB = new int[WindowSize];
|
||||
|
||||
private float ResolveValue(string value)
|
||||
|
||||
private int ResolveValue(string shite)
|
||||
{
|
||||
// Checks if the value contains any letters
|
||||
if (!value.Any(char.IsLetter))
|
||||
if (shite.Any(char.IsLetter))
|
||||
{
|
||||
float.TryParse(value, out float result);
|
||||
return result;
|
||||
switch (shite)
|
||||
{
|
||||
case "ACC":
|
||||
return Accumulator;
|
||||
case "RAX":
|
||||
return StackX;
|
||||
case "RAY":
|
||||
return StackY;
|
||||
}
|
||||
Variables.TryGetValue(shite, out int a);
|
||||
return a;
|
||||
}
|
||||
else
|
||||
{
|
||||
Variables.TryGetValue(value, out float result);
|
||||
return result;
|
||||
int.TryParse(shite, out int a);
|
||||
return a;
|
||||
}
|
||||
}
|
||||
public float ExecuteCommand(ComponentType type, string val1, string val2)
|
||||
|
||||
|
||||
public void ExecuteCommand(ComponentType type, string[] args)
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
default: return 2;
|
||||
case ComponentType.EQ_ADD:
|
||||
float a = ResolveValue(val1);
|
||||
float b = ResolveValue(val2);
|
||||
return a + b;
|
||||
case ComponentType.EQ_SUB:
|
||||
float aS = ResolveValue(val1);
|
||||
float bS = ResolveValue(val2);
|
||||
return aS - bS;
|
||||
case ComponentType.EQ_MUL:
|
||||
float aM = ResolveValue(val1);
|
||||
float bM = ResolveValue(val2);
|
||||
return aM * bM;
|
||||
case ComponentType.EQ_DIV:
|
||||
float aD = ResolveValue(val1);
|
||||
float bD = ResolveValue(val2);
|
||||
return aD / bD;
|
||||
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;
|
||||
|
||||
case ComponentType.QZ_VAR:
|
||||
float val = 0.0f;
|
||||
float.TryParse(val2, out val);
|
||||
if (Variables.ContainsKey(val1))
|
||||
{
|
||||
Variables[val1] = val;
|
||||
}
|
||||
else
|
||||
{
|
||||
Variables.Add(val1, val);
|
||||
}
|
||||
return val;
|
||||
|
||||
// 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;
|
||||
|
||||
// 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;
|
||||
|
||||
|
||||
|
||||
// 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 float EquParserLoop(string equ)
|
||||
public void EquParserLoop(string equ)
|
||||
{
|
||||
if (equ != "")
|
||||
string[] lines = equ.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
|
||||
while (pointer < lines.Length)
|
||||
{
|
||||
string[] tokens = equ.Split(new char[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
if (tokens.Length >= 3)
|
||||
string[] tokens = lines[pointer].Split(new char[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
string command = tokens[0];
|
||||
if (tokens.Length == 0)
|
||||
{
|
||||
string command = tokens[0]; // "ADD" or "VAR"
|
||||
string val1 = tokens[1]; // "X"
|
||||
string val2 = tokens[2]; // "0" or "10"
|
||||
|
||||
switch (command)
|
||||
{
|
||||
default: return 0;
|
||||
case "ADD":
|
||||
Console.WriteLine("[CommandParser] Executed command ADD");
|
||||
return ExecuteCommand(ComponentType.EQ_ADD, val1, val2);
|
||||
case "SUB":
|
||||
Console.WriteLine("[CommandParser] Executed command ADD");
|
||||
return ExecuteCommand(ComponentType.EQ_SUB, val1, val2);
|
||||
case "MUL":
|
||||
Console.WriteLine("[CommandParser] Executed command ADD");
|
||||
return ExecuteCommand(ComponentType.EQ_MUL, val1, val2);
|
||||
case "DIV":
|
||||
Console.WriteLine("[CommandParser] Executed command ADD");
|
||||
return ExecuteCommand(ComponentType.EQ_DIV, val1, val2);
|
||||
case "VAR":
|
||||
Console.WriteLine("[CommandParser] Executed command ADD");
|
||||
return ExecuteCommand(ComponentType.QZ_VAR, val1, val2);
|
||||
}
|
||||
pointer++;
|
||||
continue;
|
||||
}
|
||||
else { return 0; }
|
||||
|
||||
switch (command)
|
||||
{
|
||||
default: break;
|
||||
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;
|
||||
|
||||
// math
|
||||
case "ADD":
|
||||
ExecuteCommand(ComponentType.ADD, tokens);
|
||||
break;
|
||||
|
||||
// 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++;
|
||||
}
|
||||
|
||||
else { return 0; }
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user