Compare commits
2 Commits
70211cecc1
...
ec32e55357
Author | SHA1 | Date |
---|---|---|
Sofia | ec32e55357 | |
Sofia | 7c5bacbf66 |
|
@ -1,5 +1,6 @@
|
|||
/target
|
||||
|
||||
*.log
|
||||
*.reidc
|
||||
|
||||
#Added by cargo
|
||||
#
|
||||
|
|
|
@ -10,5 +10,10 @@ categories = ["command-line-utilities", "parsing"]
|
|||
keywords = ["compiler", "language", "scripting", "parsing", "virtual machine"]
|
||||
edition = "2018"
|
||||
|
||||
[features]
|
||||
default = ["compiler"]
|
||||
|
||||
compiler = []
|
||||
|
||||
[dependencies]
|
||||
argh = "0.1.3"
|
|
@ -1 +1,2 @@
|
|||
let gotus = 3;
|
||||
let gotus = "Hello, world!";
|
||||
print(gotus);
|
28
src/args.rs
28
src/args.rs
|
@ -5,16 +5,25 @@ use argh::FromArgs;
|
|||
#[derive(FromArgs, PartialEq, Debug)]
|
||||
#[argh(description = "reid compiler and Virtual Machine")]
|
||||
pub struct MainOpt {
|
||||
#[cfg(feature = "compiler")]
|
||||
#[argh(positional, description = "run compiled .reidc from <path>")]
|
||||
pub run_path: Option<PathBuf>,
|
||||
|
||||
#[cfg(not(feature = "compiler"))]
|
||||
#[argh(positional, description = "run compiled .reidc from <path>")]
|
||||
pub run_path: PathBuf,
|
||||
|
||||
#[argh(subcommand)]
|
||||
pub subcommand: Subcommand,
|
||||
#[cfg(feature = "compiler")]
|
||||
pub subcommand: Option<Subcommand>,
|
||||
}
|
||||
|
||||
#[cfg(feature = "compiler")]
|
||||
#[derive(FromArgs, PartialEq, Debug)]
|
||||
#[argh(subcommand)]
|
||||
pub enum Subcommand {
|
||||
Compile(Compile),
|
||||
Run(Run),
|
||||
CompileAndRun(CompileAndRun),
|
||||
}
|
||||
|
||||
#[derive(FromArgs, PartialEq, Debug)]
|
||||
|
@ -27,27 +36,16 @@ pub struct Compile {
|
|||
#[argh(positional, description = "source .reid path")]
|
||||
pub source: String,
|
||||
#[argh(positional, description = "output .reidc path")]
|
||||
pub output: String,
|
||||
pub output: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(FromArgs, PartialEq, Debug)]
|
||||
#[argh(
|
||||
subcommand,
|
||||
name = "run",
|
||||
description = "run compiled .reidc from <path>"
|
||||
)]
|
||||
pub struct Run {
|
||||
#[argh(positional, description = "otus 2")]
|
||||
pub path: String,
|
||||
}
|
||||
|
||||
#[derive(FromArgs, PartialEq, Debug)]
|
||||
#[argh(
|
||||
subcommand,
|
||||
name = "c_run",
|
||||
description = "compile and run given .reid file"
|
||||
)]
|
||||
pub struct CompileAndRun {
|
||||
pub struct Run {
|
||||
#[argh(positional, description = "source .reid path")]
|
||||
pub source: PathBuf,
|
||||
}
|
||||
|
|
|
@ -3,27 +3,10 @@ use std::collections::HashMap;
|
|||
use std::num::ParseIntError;
|
||||
|
||||
use super::errors::CompilerError;
|
||||
use super::parser::{Expression, LiteralPattern, ParsedReid, Pattern, Position, Statement};
|
||||
use super::vm::{FunctionSignature, VariableType};
|
||||
use super::parser::{Expression, LiteralPattern, ParsedReid, Pattern, Statement};
|
||||
use super::vm::{Command, CompiledReid, FuncID, FunctionSignature, HeapID, Position, VariableType};
|
||||
|
||||
type Variable = (HeapID, VariableType);
|
||||
pub type FuncID = usize;
|
||||
pub type HeapID = usize;
|
||||
pub type RegID = usize;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Command {
|
||||
InitializeVariable(HeapID, VariableType), // Initializes new variable to HeapID at VariableType
|
||||
BeginScope, // Begins new Scope
|
||||
EndScope, // Ends Scope
|
||||
Pop(RegID), // Pop into registery at RegID
|
||||
Push(RegID), // Push out of registery at RegID
|
||||
AssignVariable(HeapID, RegID), // Assign variable from registery at RegID
|
||||
VarToReg(HeapID, RegID), // Bring Variable to registery at RegID
|
||||
StringLit(String), // Bring String Literal to Stack
|
||||
I32Lit(i32), // Bring i32 Literal to Stack
|
||||
FunctionCall(FuncID), // Call Function at FuncID
|
||||
}
|
||||
pub type Variable = (HeapID, VariableType);
|
||||
|
||||
pub struct Compiler {
|
||||
parsed: ParsedReid,
|
||||
|
@ -31,11 +14,6 @@ pub struct Compiler {
|
|||
list: Vec<Command>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct CompiledReid {
|
||||
pub list: Vec<Command>,
|
||||
}
|
||||
|
||||
impl Compiler {
|
||||
pub fn from(parsed: ParsedReid) -> Compiler {
|
||||
Compiler {
|
||||
|
@ -192,11 +170,11 @@ impl Scope {
|
|||
}
|
||||
}
|
||||
|
||||
fn find_function(&self, signature: &FunctionSignature) -> Option<(usize, &FunctionSignature)> {
|
||||
fn find_function(&self, signature: &FunctionSignature) -> Option<(FuncID, &FunctionSignature)> {
|
||||
let mut found = None;
|
||||
for (idx, func) in self.functions.iter().enumerate() {
|
||||
if func == signature {
|
||||
found = Some((idx, func));
|
||||
found = Some((idx as u16, func));
|
||||
}
|
||||
}
|
||||
found
|
||||
|
|
|
@ -1,14 +1,22 @@
|
|||
use super::parser::Position;
|
||||
use std::fmt;
|
||||
use std::fmt::Display;
|
||||
use std::io;
|
||||
|
||||
#[cfg(feature = "compiler")]
|
||||
use std::num::ParseIntError;
|
||||
|
||||
#[cfg(feature = "compiler")]
|
||||
use super::vm::Position;
|
||||
use super::vm::VariableType;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum GenericError {
|
||||
StdIOError(io::Error),
|
||||
CorruptedBytecode,
|
||||
#[cfg(feature = "compiler")]
|
||||
SyntaxError(SyntaxError),
|
||||
#[cfg(feature = "compiler")]
|
||||
CompilerError(CompilerError),
|
||||
}
|
||||
|
||||
impl From<io::Error> for GenericError {
|
||||
|
@ -17,7 +25,38 @@ impl From<io::Error> for GenericError {
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "compiler")]
|
||||
impl From<SyntaxError> for GenericError {
|
||||
fn from(error: SyntaxError) -> Self {
|
||||
Self::SyntaxError(error)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "compiler")]
|
||||
impl From<CompilerError> for GenericError {
|
||||
fn from(error: CompilerError) -> Self {
|
||||
Self::CompilerError(error)
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for GenericError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
let text = match self {
|
||||
GenericError::StdIOError(err) => format!("IO Error: {}", err),
|
||||
GenericError::CorruptedBytecode => {
|
||||
"Failed to read bytecode. Bytecode might be corrupted.".to_string()
|
||||
}
|
||||
#[cfg(feature = "compiler")]
|
||||
GenericError::SyntaxError(err) => format!("Syntax Error: {}", err),
|
||||
#[cfg(feature = "compiler")]
|
||||
GenericError::CompilerError(err) => format!("Compiler Error: {}", err),
|
||||
};
|
||||
write!(f, "{}", text)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
#[cfg(feature = "compiler")]
|
||||
pub enum SyntaxError {
|
||||
#[allow(dead_code)]
|
||||
Fatal,
|
||||
|
@ -28,6 +67,7 @@ pub enum SyntaxError {
|
|||
ExpectedPattern(Position),
|
||||
}
|
||||
|
||||
#[cfg(feature = "compiler")]
|
||||
impl SyntaxError {
|
||||
fn from_opt(from: &Option<Box<SyntaxError>>) -> String {
|
||||
if let Some(err) = from {
|
||||
|
@ -38,6 +78,7 @@ impl SyntaxError {
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "compiler")]
|
||||
impl Display for SyntaxError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
let text = match self {
|
||||
|
@ -61,6 +102,7 @@ impl Display for SyntaxError {
|
|||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
#[cfg(feature = "compiler")]
|
||||
pub enum CompilerError {
|
||||
#[allow(dead_code)]
|
||||
Fatal,
|
||||
|
@ -73,6 +115,7 @@ pub enum CompilerError {
|
|||
ParseIntError(ParseIntError),
|
||||
}
|
||||
|
||||
#[cfg(feature = "compiler")]
|
||||
impl Display for CompilerError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
let text = match self {
|
||||
|
|
161
src/file_io.rs
161
src/file_io.rs
|
@ -4,11 +4,170 @@ use std::io::BufReader;
|
|||
use std::path::Path;
|
||||
|
||||
use super::errors::GenericError;
|
||||
use super::vm::VariableType;
|
||||
use super::vm::{Command, CompiledReid};
|
||||
|
||||
pub fn open_file(path: &Path) -> Result<String, GenericError> {
|
||||
#[cfg(feature = "compiler")]
|
||||
pub fn open_source(path: &Path) -> Result<String, GenericError> {
|
||||
let file = File::open(path)?;
|
||||
let mut reader = BufReader::new(file);
|
||||
let mut text = String::new();
|
||||
reader.read_to_string(&mut text)?;
|
||||
Ok(text)
|
||||
}
|
||||
|
||||
#[cfg(feature = "compiler")]
|
||||
pub fn into_bytecode(compiled: &CompiledReid) -> Vec<u8> {
|
||||
let mut list = Vec::new();
|
||||
let iter = compiled.list.iter();
|
||||
for item in iter {
|
||||
list.append(&mut item.into_u8());
|
||||
}
|
||||
list
|
||||
}
|
||||
|
||||
#[cfg(feature = "compiler")]
|
||||
pub fn write_bytecode(bytecode: Vec<u8>, path: &Path) -> Result<(), GenericError> {
|
||||
let mut file = File::create(path)?;
|
||||
file.write_all(&bytecode)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn open_bytecode(path: &Path) -> Result<CompiledReid, GenericError> {
|
||||
let file = File::open(path)?;
|
||||
let mut reader = BufReader::new(file);
|
||||
let mut commands = Vec::new();
|
||||
let mut iter = reader.fill_buf()?.iter().peekable();
|
||||
while iter.peek().is_some() {
|
||||
if let Some(command) = Command::from_u8(&mut iter) {
|
||||
commands.push(command);
|
||||
} else {
|
||||
return Err(GenericError::CorruptedBytecode);
|
||||
}
|
||||
}
|
||||
Ok(CompiledReid { list: commands })
|
||||
}
|
||||
|
||||
impl VariableType {
|
||||
#[cfg(feature = "compiler")]
|
||||
fn into_u8(&self) -> u8 {
|
||||
match self {
|
||||
VariableType::TypeString => 0,
|
||||
VariableType::TypeI32 => 1,
|
||||
}
|
||||
}
|
||||
|
||||
fn from_u8(num: u8) -> Option<VariableType> {
|
||||
match num {
|
||||
0 => Some(VariableType::TypeString),
|
||||
1 => Some(VariableType::TypeI32),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Command {
|
||||
#[cfg(feature = "compiler")]
|
||||
fn id(&self) -> u8 {
|
||||
match *self {
|
||||
Command::InitializeVariable(..) => 0,
|
||||
Command::BeginScope => 1,
|
||||
Command::EndScope => 2,
|
||||
Command::Pop(..) => 3,
|
||||
Command::Push(..) => 4,
|
||||
Command::AssignVariable(..) => 5,
|
||||
Command::VarToReg(..) => 6,
|
||||
Command::StringLit(..) => 7,
|
||||
Command::I32Lit(..) => 8,
|
||||
Command::FunctionCall(..) => 9,
|
||||
}
|
||||
}
|
||||
|
||||
fn from_u8<'a, T: Iterator<Item = &'a u8>>(iter: &mut T) -> Option<Command> {
|
||||
let id = iter.next()?;
|
||||
match id {
|
||||
0 => {
|
||||
let heapid = u16::from_be_bytes([*iter.next()?, *iter.next()?]);
|
||||
Some(Command::InitializeVariable(
|
||||
heapid,
|
||||
VariableType::from_u8(*iter.next()?)?,
|
||||
))
|
||||
}
|
||||
1 => Some(Command::BeginScope),
|
||||
2 => Some(Command::EndScope),
|
||||
3 => Some(Command::Pop(*iter.next()?)),
|
||||
4 => Some(Command::Push(*iter.next()?)),
|
||||
5 => {
|
||||
let heapid = u16::from_be_bytes([*iter.next()?, *iter.next()?]);
|
||||
Some(Command::AssignVariable(heapid, *iter.next()?))
|
||||
}
|
||||
6 => {
|
||||
let heapid = u16::from_be_bytes([*iter.next()?, *iter.next()?]);
|
||||
Some(Command::VarToReg(heapid, *iter.next()?))
|
||||
}
|
||||
7 => {
|
||||
let len = u32::from_be_bytes([
|
||||
*iter.next()?,
|
||||
*iter.next()?,
|
||||
*iter.next()?,
|
||||
*iter.next()?,
|
||||
]);
|
||||
let string = String::from_utf8(iter.take(len as usize).cloned().collect()).ok()?;
|
||||
Some(Command::StringLit(string))
|
||||
}
|
||||
8 => {
|
||||
let num = i32::from_be_bytes([
|
||||
*iter.next()?,
|
||||
*iter.next()?,
|
||||
*iter.next()?,
|
||||
*iter.next()?,
|
||||
]);
|
||||
Some(Command::I32Lit(num))
|
||||
}
|
||||
9 => {
|
||||
let funcid = u16::from_be_bytes([*iter.next()?, *iter.next()?]);
|
||||
Some(Command::FunctionCall(funcid))
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "compiler")]
|
||||
fn into_u8(&self) -> Vec<u8> {
|
||||
let mut list = Vec::new();
|
||||
list.push(self.id());
|
||||
match &self {
|
||||
Command::InitializeVariable(heapid, variabletype) => {
|
||||
let heapid = heapid.to_be_bytes();
|
||||
list.append(&mut vec![heapid[0], heapid[1], variabletype.into_u8()])
|
||||
}
|
||||
Command::BeginScope => (),
|
||||
Command::EndScope => (),
|
||||
Command::Pop(regid) => list.push(*regid),
|
||||
Command::Push(regid) => list.push(*regid),
|
||||
Command::AssignVariable(heapid, regid) => {
|
||||
let heapid = heapid.to_be_bytes();
|
||||
list.append(&mut vec![heapid[0], heapid[1], *regid]);
|
||||
}
|
||||
Command::VarToReg(heapid, regid) => {
|
||||
let heapid = heapid.to_be_bytes();
|
||||
list.append(&mut vec![heapid[0], heapid[1], *regid]);
|
||||
}
|
||||
Command::StringLit(string) => {
|
||||
let string = string.as_bytes();
|
||||
let len = (string.len() as u32).to_be_bytes();
|
||||
list.append(&mut vec![len[0], len[1], len[2], len[3]]);
|
||||
list.append(&mut string.to_vec());
|
||||
}
|
||||
Command::I32Lit(num) => {
|
||||
let num = num.to_be_bytes();
|
||||
list.append(&mut vec![num[0], num[1], num[2], num[3]]);
|
||||
}
|
||||
Command::FunctionCall(funcid) => {
|
||||
let funcid = funcid.to_be_bytes();
|
||||
list.append(&mut vec![funcid[0], funcid[1]]);
|
||||
}
|
||||
}
|
||||
list
|
||||
}
|
||||
}
|
||||
|
|
110
src/main.rs
110
src/main.rs
|
@ -2,23 +2,34 @@
|
|||
#![warn(clippy::all)]
|
||||
|
||||
mod args;
|
||||
#[cfg(feature = "compiler")]
|
||||
mod compiler;
|
||||
mod errors;
|
||||
mod file_io;
|
||||
#[cfg(feature = "compiler")]
|
||||
mod parser;
|
||||
mod vm;
|
||||
|
||||
use file_io::open_file;
|
||||
use file_io::open_bytecode;
|
||||
#[cfg(feature = "compiler")]
|
||||
use file_io::{into_bytecode, open_source, write_bytecode};
|
||||
#[cfg(feature = "compiler")]
|
||||
use std::env;
|
||||
use std::path::Path;
|
||||
#[cfg(feature = "compiler")]
|
||||
use std::path::PathBuf;
|
||||
|
||||
use args::*;
|
||||
use compiler::{CompiledReid, Compiler};
|
||||
#[cfg(feature = "compiler")]
|
||||
use compiler::Compiler;
|
||||
#[cfg(feature = "compiler")]
|
||||
use errors::GenericError;
|
||||
#[cfg(feature = "compiler")]
|
||||
use parser::Parser;
|
||||
use vm::{
|
||||
BuiltinFunctionDef, BuiltinFunctions, FunctionSignature, Value, VariableType, VirtualMachine,
|
||||
};
|
||||
#[cfg(feature = "compiler")]
|
||||
use vm::FunctionSignature;
|
||||
use vm::{BuiltinFunctionDef, BuiltinFunctions, CompiledReid, Value, VariableType, VirtualMachine};
|
||||
|
||||
// cargo run c_run reid_src/test.reid for previous functionality
|
||||
fn main() {
|
||||
let print = BuiltinFunctionDef::new(
|
||||
"print",
|
||||
|
@ -33,35 +44,78 @@ fn main() {
|
|||
|
||||
let opt: MainOpt = argh::from_env();
|
||||
|
||||
match opt.subcommand {
|
||||
Subcommand::Compile(_) => {}
|
||||
Subcommand::Run(_) => {}
|
||||
Subcommand::CompileAndRun(c_run) => {
|
||||
let path = Path::new(&c_run.source);
|
||||
let compiled = compile(path, builtin_functions.signatures());
|
||||
run(compiled, builtin_functions);
|
||||
#[cfg(feature = "compiler")]
|
||||
if let Some(run_path) = opt.run_path {
|
||||
run_bytecode(&run_path, builtin_functions);
|
||||
} else if let Some(subcommand) = opt.subcommand {
|
||||
match subcommand {
|
||||
Subcommand::Compile(opt) => {
|
||||
let source = Path::new(&opt.source);
|
||||
let output = if let Some(output) = opt.output {
|
||||
PathBuf::from(output)
|
||||
} else {
|
||||
source.with_extension("reidc")
|
||||
};
|
||||
let compiled = compile(source, builtin_functions.signatures());
|
||||
match compiled {
|
||||
Ok(compiled) => {
|
||||
let bytecode = into_bytecode(&compiled);
|
||||
if let Err(err) = write_bytecode(bytecode, &output) {
|
||||
eprintln!("{}", err);
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
Err(error) => {
|
||||
eprintln!("{}", error);
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
Subcommand::Run(opt) => {
|
||||
let path = Path::new(&opt.source);
|
||||
let compiled = compile(path, builtin_functions.signatures());
|
||||
match compiled {
|
||||
Ok(compiled) => run(compiled, builtin_functions),
|
||||
Err(error) => {
|
||||
eprintln!("{}", error);
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
let command = env::args().collect::<Vec<String>>().join(" ");
|
||||
eprintln!("Please try running instead:");
|
||||
eprintln!(" {} <path>", command);
|
||||
eprintln!(" {} help", command);
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "compiler"))]
|
||||
run_bytecode(&opt.run_path, builtin_functions);
|
||||
}
|
||||
|
||||
fn run_bytecode(run_path: &Path, builtin_functions: BuiltinFunctions) {
|
||||
let compiled = open_bytecode(run_path);
|
||||
match compiled {
|
||||
Ok(compiled) => run(compiled, builtin_functions),
|
||||
Err(error) => {
|
||||
eprintln!("{}", error);
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn compile(path: &Path, builtin: Vec<FunctionSignature>) -> CompiledReid {
|
||||
let parsed = Parser::from(open_file(&path).ok().unwrap()).parse();
|
||||
if let Err(error) = parsed {
|
||||
eprintln!("Syntax error: {}", error);
|
||||
std::process::exit(1);
|
||||
}
|
||||
dbg!(&parsed);
|
||||
#[cfg(feature = "compiler")]
|
||||
fn compile(path: &Path, builtin: Vec<FunctionSignature>) -> Result<CompiledReid, GenericError> {
|
||||
let parsed = Parser::from(open_source(&path)?).parse()?;
|
||||
//dbg!(&parsed);
|
||||
|
||||
let compiled = Compiler::from(parsed.unwrap())
|
||||
let compiled = Compiler::from(parsed)
|
||||
.with_builtin_functions(builtin)
|
||||
.compile();
|
||||
if let Err(error) = compiled {
|
||||
eprintln!("Compilation error: {}", error);
|
||||
std::process::exit(1);
|
||||
}
|
||||
dbg!(&compiled);
|
||||
.compile()?;
|
||||
//dbg!(&compiled);
|
||||
|
||||
compiled.unwrap()
|
||||
Ok(compiled)
|
||||
}
|
||||
|
||||
fn run(reid: CompiledReid, builtin: BuiltinFunctions) {
|
||||
|
|
|
@ -1,9 +1,8 @@
|
|||
mod parsed_reid;
|
||||
|
||||
use super::errors::SyntaxError;
|
||||
use super::vm::Position;
|
||||
pub use parsed_reid::*;
|
||||
use std::fmt;
|
||||
use std::fmt::Display;
|
||||
|
||||
const ALLOWED_IDENT_CHARS: [char; 38] = [
|
||||
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
|
||||
|
@ -312,12 +311,3 @@ impl<'a> Expects<'a> {
|
|||
self.texts
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
pub struct Position(usize, usize);
|
||||
|
||||
impl Display for Position {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "line {}, column {}", self.0, self.1)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,64 @@
|
|||
use std::fmt;
|
||||
use std::fmt::Display;
|
||||
|
||||
pub type FuncID = u16;
|
||||
pub type HeapID = u16;
|
||||
pub type RegID = u8;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Command {
|
||||
InitializeVariable(HeapID, VariableType), // Initializes new variable to HeapID at VariableType
|
||||
BeginScope, // Begins new Scope
|
||||
EndScope, // Ends Scope
|
||||
Pop(RegID), // Pop into registery at RegID
|
||||
Push(RegID), // Push out of registery at RegID
|
||||
AssignVariable(HeapID, RegID), // Assign variable from registery at RegID
|
||||
VarToReg(HeapID, RegID), // Bring Variable to registery at RegID
|
||||
StringLit(String), // Bring String Literal to Stack
|
||||
I32Lit(i32), // Bring i32 Literal to Stack
|
||||
FunctionCall(FuncID), // Call Function at FuncID
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct CompiledReid {
|
||||
pub list: Vec<Command>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub enum Value {
|
||||
StringVal(String),
|
||||
I32Val(i32),
|
||||
}
|
||||
|
||||
impl Value {
|
||||
pub fn get_type(&self) -> VariableType {
|
||||
match self {
|
||||
Value::StringVal(_) => VariableType::TypeString,
|
||||
Value::I32Val(_) => VariableType::TypeI32,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
||||
pub enum VariableType {
|
||||
TypeString,
|
||||
TypeI32,
|
||||
}
|
||||
|
||||
impl ToString for VariableType {
|
||||
fn to_string(&self) -> String {
|
||||
match self {
|
||||
VariableType::TypeString => "String".to_string(),
|
||||
VariableType::TypeI32 => "i32".to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
pub struct Position(pub usize, pub usize);
|
||||
|
||||
impl Display for Position {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "line {}, column {}", self.0, self.1)
|
||||
}
|
||||
}
|
|
@ -1,10 +1,11 @@
|
|||
pub mod compiled;
|
||||
pub mod functions;
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use super::compiler::{Command, CompiledReid, HeapID};
|
||||
use super::errors::RuntimePanic;
|
||||
|
||||
pub use compiled::*;
|
||||
pub use functions::*;
|
||||
|
||||
pub struct VirtualMachine {
|
||||
|
@ -92,7 +93,7 @@ impl VirtualMachine {
|
|||
}
|
||||
Command::Pop(regid) => {
|
||||
if let Some(val) = self.stack.pop() {
|
||||
self.registry[regid] = Some(val);
|
||||
self.registry[regid as usize] = Some(val);
|
||||
//dbg!("Registry popped", regid, &self.stack, &self.registry);
|
||||
Ok(())
|
||||
} else {
|
||||
|
@ -100,7 +101,7 @@ impl VirtualMachine {
|
|||
}
|
||||
}
|
||||
Command::Push(regid) => {
|
||||
if let Some(reg) = &self.registry[regid] {
|
||||
if let Some(reg) = &self.registry[regid as usize] {
|
||||
if self.stack.len() < usize::MAX {
|
||||
self.stack.push(reg.clone());
|
||||
//dbg!("Registry pushed", regid, &self.stack);
|
||||
|
@ -113,7 +114,7 @@ impl VirtualMachine {
|
|||
}
|
||||
}
|
||||
Command::AssignVariable(heapid, regid) => {
|
||||
if let Some(reg) = &self.registry[regid] {
|
||||
if let Some(reg) = &self.registry[regid as usize] {
|
||||
if let Some(var) = self.heap.get_mut(&heapid) {
|
||||
var.try_set(Some(reg.clone()))?;
|
||||
//dbg!("Variable assigned", heapid, regid, &self.heap);
|
||||
|
@ -128,7 +129,7 @@ impl VirtualMachine {
|
|||
Command::VarToReg(heapid, regid) => {
|
||||
if let Some(var) = self.heap.get(&heapid) {
|
||||
if let Some(val) = &var.1 {
|
||||
self.registry[regid] = Some(val.clone());
|
||||
self.registry[regid as usize] = Some(val.clone());
|
||||
//dbg!("Variable pushed to registry", heapid, regid, &self.registry);
|
||||
Ok(())
|
||||
} else {
|
||||
|
@ -156,10 +157,10 @@ impl VirtualMachine {
|
|||
}
|
||||
}
|
||||
Command::FunctionCall(funcid) => {
|
||||
if self.functions.len() <= funcid {
|
||||
if self.functions.len() <= funcid as usize {
|
||||
Err(RuntimePanic::InvalidFuncAddress)
|
||||
} else {
|
||||
match &self.functions[funcid] {
|
||||
match &self.functions[funcid as usize] {
|
||||
FunctionDef::Builtin(f) => {
|
||||
let mut params = Vec::new();
|
||||
for _ in 0..f.signature.parameters.len() {
|
||||
|
@ -197,32 +198,3 @@ impl AllocatedVar {
|
|||
}
|
||||
}
|
||||
}
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub enum Value {
|
||||
StringVal(String),
|
||||
I32Val(i32),
|
||||
}
|
||||
|
||||
impl Value {
|
||||
fn get_type(&self) -> VariableType {
|
||||
match self {
|
||||
Value::StringVal(_) => VariableType::TypeString,
|
||||
Value::I32Val(_) => VariableType::TypeI32,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
||||
pub enum VariableType {
|
||||
TypeString,
|
||||
TypeI32,
|
||||
}
|
||||
|
||||
impl ToString for VariableType {
|
||||
fn to_string(&self) -> String {
|
||||
match self {
|
||||
VariableType::TypeString => "String".to_string(),
|
||||
VariableType::TypeI32 => "i32".to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue