Compare commits
No commits in common. "ec32e55357f188d73385e59aa48949d87bbba789" and "70211cecc1ad769719dcbeb2992058209c893654" have entirely different histories.
ec32e55357
...
70211cecc1
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,6 +1,5 @@
|
|||||||
/target
|
/target
|
||||||
*.log
|
|
||||||
*.reidc
|
|
||||||
|
|
||||||
#Added by cargo
|
#Added by cargo
|
||||||
#
|
#
|
||||||
|
@ -10,10 +10,5 @@ categories = ["command-line-utilities", "parsing"]
|
|||||||
keywords = ["compiler", "language", "scripting", "parsing", "virtual machine"]
|
keywords = ["compiler", "language", "scripting", "parsing", "virtual machine"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
[features]
|
|
||||||
default = ["compiler"]
|
|
||||||
|
|
||||||
compiler = []
|
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
argh = "0.1.3"
|
argh = "0.1.3"
|
@ -1,2 +1 @@
|
|||||||
let gotus = "Hello, world!";
|
let gotus = 3;
|
||||||
print(gotus);
|
|
28
src/args.rs
28
src/args.rs
@ -5,25 +5,16 @@ use argh::FromArgs;
|
|||||||
#[derive(FromArgs, PartialEq, Debug)]
|
#[derive(FromArgs, PartialEq, Debug)]
|
||||||
#[argh(description = "reid compiler and Virtual Machine")]
|
#[argh(description = "reid compiler and Virtual Machine")]
|
||||||
pub struct MainOpt {
|
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)]
|
#[argh(subcommand)]
|
||||||
#[cfg(feature = "compiler")]
|
pub subcommand: Subcommand,
|
||||||
pub subcommand: Option<Subcommand>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "compiler")]
|
|
||||||
#[derive(FromArgs, PartialEq, Debug)]
|
#[derive(FromArgs, PartialEq, Debug)]
|
||||||
#[argh(subcommand)]
|
#[argh(subcommand)]
|
||||||
pub enum Subcommand {
|
pub enum Subcommand {
|
||||||
Compile(Compile),
|
Compile(Compile),
|
||||||
Run(Run),
|
Run(Run),
|
||||||
|
CompileAndRun(CompileAndRun),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(FromArgs, PartialEq, Debug)]
|
#[derive(FromArgs, PartialEq, Debug)]
|
||||||
@ -36,16 +27,27 @@ pub struct Compile {
|
|||||||
#[argh(positional, description = "source .reid path")]
|
#[argh(positional, description = "source .reid path")]
|
||||||
pub source: String,
|
pub source: String,
|
||||||
#[argh(positional, description = "output .reidc path")]
|
#[argh(positional, description = "output .reidc path")]
|
||||||
pub output: Option<String>,
|
pub output: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(FromArgs, PartialEq, Debug)]
|
#[derive(FromArgs, PartialEq, Debug)]
|
||||||
#[argh(
|
#[argh(
|
||||||
subcommand,
|
subcommand,
|
||||||
name = "run",
|
name = "run",
|
||||||
description = "compile and run given .reid file"
|
description = "run compiled .reidc from <path>"
|
||||||
)]
|
)]
|
||||||
pub struct Run {
|
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 {
|
||||||
#[argh(positional, description = "source .reid path")]
|
#[argh(positional, description = "source .reid path")]
|
||||||
pub source: PathBuf,
|
pub source: PathBuf,
|
||||||
}
|
}
|
||||||
|
@ -3,10 +3,27 @@ use std::collections::HashMap;
|
|||||||
use std::num::ParseIntError;
|
use std::num::ParseIntError;
|
||||||
|
|
||||||
use super::errors::CompilerError;
|
use super::errors::CompilerError;
|
||||||
use super::parser::{Expression, LiteralPattern, ParsedReid, Pattern, Statement};
|
use super::parser::{Expression, LiteralPattern, ParsedReid, Pattern, Position, Statement};
|
||||||
use super::vm::{Command, CompiledReid, FuncID, FunctionSignature, HeapID, Position, VariableType};
|
use super::vm::{FunctionSignature, VariableType};
|
||||||
|
|
||||||
pub type Variable = (HeapID, 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 struct Compiler {
|
pub struct Compiler {
|
||||||
parsed: ParsedReid,
|
parsed: ParsedReid,
|
||||||
@ -14,6 +31,11 @@ pub struct Compiler {
|
|||||||
list: Vec<Command>,
|
list: Vec<Command>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct CompiledReid {
|
||||||
|
pub list: Vec<Command>,
|
||||||
|
}
|
||||||
|
|
||||||
impl Compiler {
|
impl Compiler {
|
||||||
pub fn from(parsed: ParsedReid) -> Compiler {
|
pub fn from(parsed: ParsedReid) -> Compiler {
|
||||||
Compiler {
|
Compiler {
|
||||||
@ -170,11 +192,11 @@ impl Scope {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn find_function(&self, signature: &FunctionSignature) -> Option<(FuncID, &FunctionSignature)> {
|
fn find_function(&self, signature: &FunctionSignature) -> Option<(usize, &FunctionSignature)> {
|
||||||
let mut found = None;
|
let mut found = None;
|
||||||
for (idx, func) in self.functions.iter().enumerate() {
|
for (idx, func) in self.functions.iter().enumerate() {
|
||||||
if func == signature {
|
if func == signature {
|
||||||
found = Some((idx as u16, func));
|
found = Some((idx, func));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
found
|
found
|
||||||
|
@ -1,22 +1,14 @@
|
|||||||
|
use super::parser::Position;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::fmt::Display;
|
use std::fmt::Display;
|
||||||
use std::io;
|
use std::io;
|
||||||
|
|
||||||
#[cfg(feature = "compiler")]
|
|
||||||
use std::num::ParseIntError;
|
use std::num::ParseIntError;
|
||||||
|
|
||||||
#[cfg(feature = "compiler")]
|
|
||||||
use super::vm::Position;
|
|
||||||
use super::vm::VariableType;
|
use super::vm::VariableType;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum GenericError {
|
pub enum GenericError {
|
||||||
StdIOError(io::Error),
|
StdIOError(io::Error),
|
||||||
CorruptedBytecode,
|
|
||||||
#[cfg(feature = "compiler")]
|
|
||||||
SyntaxError(SyntaxError),
|
|
||||||
#[cfg(feature = "compiler")]
|
|
||||||
CompilerError(CompilerError),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<io::Error> for GenericError {
|
impl From<io::Error> for GenericError {
|
||||||
@ -25,38 +17,7 @@ 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)]
|
#[derive(Debug)]
|
||||||
#[cfg(feature = "compiler")]
|
|
||||||
pub enum SyntaxError {
|
pub enum SyntaxError {
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
Fatal,
|
Fatal,
|
||||||
@ -67,7 +28,6 @@ pub enum SyntaxError {
|
|||||||
ExpectedPattern(Position),
|
ExpectedPattern(Position),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "compiler")]
|
|
||||||
impl SyntaxError {
|
impl SyntaxError {
|
||||||
fn from_opt(from: &Option<Box<SyntaxError>>) -> String {
|
fn from_opt(from: &Option<Box<SyntaxError>>) -> String {
|
||||||
if let Some(err) = from {
|
if let Some(err) = from {
|
||||||
@ -78,7 +38,6 @@ impl SyntaxError {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "compiler")]
|
|
||||||
impl Display for SyntaxError {
|
impl Display for SyntaxError {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
let text = match self {
|
let text = match self {
|
||||||
@ -102,7 +61,6 @@ impl Display for SyntaxError {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
#[cfg(feature = "compiler")]
|
|
||||||
pub enum CompilerError {
|
pub enum CompilerError {
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
Fatal,
|
Fatal,
|
||||||
@ -115,7 +73,6 @@ pub enum CompilerError {
|
|||||||
ParseIntError(ParseIntError),
|
ParseIntError(ParseIntError),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "compiler")]
|
|
||||||
impl Display for CompilerError {
|
impl Display for CompilerError {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
let text = match self {
|
let text = match self {
|
||||||
|
161
src/file_io.rs
161
src/file_io.rs
@ -4,170 +4,11 @@ use std::io::BufReader;
|
|||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
use super::errors::GenericError;
|
use super::errors::GenericError;
|
||||||
use super::vm::VariableType;
|
|
||||||
use super::vm::{Command, CompiledReid};
|
|
||||||
|
|
||||||
#[cfg(feature = "compiler")]
|
pub fn open_file(path: &Path) -> Result<String, GenericError> {
|
||||||
pub fn open_source(path: &Path) -> Result<String, GenericError> {
|
|
||||||
let file = File::open(path)?;
|
let file = File::open(path)?;
|
||||||
let mut reader = BufReader::new(file);
|
let mut reader = BufReader::new(file);
|
||||||
let mut text = String::new();
|
let mut text = String::new();
|
||||||
reader.read_to_string(&mut text)?;
|
reader.read_to_string(&mut text)?;
|
||||||
Ok(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,34 +2,23 @@
|
|||||||
#![warn(clippy::all)]
|
#![warn(clippy::all)]
|
||||||
|
|
||||||
mod args;
|
mod args;
|
||||||
#[cfg(feature = "compiler")]
|
|
||||||
mod compiler;
|
mod compiler;
|
||||||
mod errors;
|
mod errors;
|
||||||
mod file_io;
|
mod file_io;
|
||||||
#[cfg(feature = "compiler")]
|
|
||||||
mod parser;
|
mod parser;
|
||||||
mod vm;
|
mod vm;
|
||||||
|
|
||||||
use file_io::open_bytecode;
|
use file_io::open_file;
|
||||||
#[cfg(feature = "compiler")]
|
|
||||||
use file_io::{into_bytecode, open_source, write_bytecode};
|
|
||||||
#[cfg(feature = "compiler")]
|
|
||||||
use std::env;
|
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
#[cfg(feature = "compiler")]
|
|
||||||
use std::path::PathBuf;
|
|
||||||
|
|
||||||
use args::*;
|
use args::*;
|
||||||
#[cfg(feature = "compiler")]
|
use compiler::{CompiledReid, Compiler};
|
||||||
use compiler::Compiler;
|
|
||||||
#[cfg(feature = "compiler")]
|
|
||||||
use errors::GenericError;
|
|
||||||
#[cfg(feature = "compiler")]
|
|
||||||
use parser::Parser;
|
use parser::Parser;
|
||||||
#[cfg(feature = "compiler")]
|
use vm::{
|
||||||
use vm::FunctionSignature;
|
BuiltinFunctionDef, BuiltinFunctions, FunctionSignature, Value, VariableType, VirtualMachine,
|
||||||
use vm::{BuiltinFunctionDef, BuiltinFunctions, CompiledReid, Value, VariableType, VirtualMachine};
|
};
|
||||||
|
|
||||||
|
// cargo run c_run reid_src/test.reid for previous functionality
|
||||||
fn main() {
|
fn main() {
|
||||||
let print = BuiltinFunctionDef::new(
|
let print = BuiltinFunctionDef::new(
|
||||||
"print",
|
"print",
|
||||||
@ -44,78 +33,35 @@ fn main() {
|
|||||||
|
|
||||||
let opt: MainOpt = argh::from_env();
|
let opt: MainOpt = argh::from_env();
|
||||||
|
|
||||||
#[cfg(feature = "compiler")]
|
match opt.subcommand {
|
||||||
if let Some(run_path) = opt.run_path {
|
Subcommand::Compile(_) => {}
|
||||||
run_bytecode(&run_path, builtin_functions);
|
Subcommand::Run(_) => {}
|
||||||
} else if let Some(subcommand) = opt.subcommand {
|
Subcommand::CompileAndRun(c_run) => {
|
||||||
match subcommand {
|
let path = Path::new(&c_run.source);
|
||||||
Subcommand::Compile(opt) => {
|
let compiled = compile(path, builtin_functions.signatures());
|
||||||
let source = Path::new(&opt.source);
|
run(compiled, builtin_functions);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "compiler")]
|
fn compile(path: &Path, builtin: Vec<FunctionSignature>) -> CompiledReid {
|
||||||
fn compile(path: &Path, builtin: Vec<FunctionSignature>) -> Result<CompiledReid, GenericError> {
|
let parsed = Parser::from(open_file(&path).ok().unwrap()).parse();
|
||||||
let parsed = Parser::from(open_source(&path)?).parse()?;
|
if let Err(error) = parsed {
|
||||||
//dbg!(&parsed);
|
eprintln!("Syntax error: {}", error);
|
||||||
|
std::process::exit(1);
|
||||||
|
}
|
||||||
|
dbg!(&parsed);
|
||||||
|
|
||||||
let compiled = Compiler::from(parsed)
|
let compiled = Compiler::from(parsed.unwrap())
|
||||||
.with_builtin_functions(builtin)
|
.with_builtin_functions(builtin)
|
||||||
.compile()?;
|
.compile();
|
||||||
//dbg!(&compiled);
|
if let Err(error) = compiled {
|
||||||
|
eprintln!("Compilation error: {}", error);
|
||||||
|
std::process::exit(1);
|
||||||
|
}
|
||||||
|
dbg!(&compiled);
|
||||||
|
|
||||||
Ok(compiled)
|
compiled.unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run(reid: CompiledReid, builtin: BuiltinFunctions) {
|
fn run(reid: CompiledReid, builtin: BuiltinFunctions) {
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
mod parsed_reid;
|
mod parsed_reid;
|
||||||
|
|
||||||
use super::errors::SyntaxError;
|
use super::errors::SyntaxError;
|
||||||
use super::vm::Position;
|
|
||||||
pub use parsed_reid::*;
|
pub use parsed_reid::*;
|
||||||
|
use std::fmt;
|
||||||
|
use std::fmt::Display;
|
||||||
|
|
||||||
const ALLOWED_IDENT_CHARS: [char; 38] = [
|
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',
|
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
|
||||||
@ -311,3 +312,12 @@ impl<'a> Expects<'a> {
|
|||||||
self.texts
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,64 +0,0 @@
|
|||||||
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,11 +1,10 @@
|
|||||||
pub mod compiled;
|
|
||||||
pub mod functions;
|
pub mod functions;
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
use super::compiler::{Command, CompiledReid, HeapID};
|
||||||
use super::errors::RuntimePanic;
|
use super::errors::RuntimePanic;
|
||||||
|
|
||||||
pub use compiled::*;
|
|
||||||
pub use functions::*;
|
pub use functions::*;
|
||||||
|
|
||||||
pub struct VirtualMachine {
|
pub struct VirtualMachine {
|
||||||
@ -93,7 +92,7 @@ impl VirtualMachine {
|
|||||||
}
|
}
|
||||||
Command::Pop(regid) => {
|
Command::Pop(regid) => {
|
||||||
if let Some(val) = self.stack.pop() {
|
if let Some(val) = self.stack.pop() {
|
||||||
self.registry[regid as usize] = Some(val);
|
self.registry[regid] = Some(val);
|
||||||
//dbg!("Registry popped", regid, &self.stack, &self.registry);
|
//dbg!("Registry popped", regid, &self.stack, &self.registry);
|
||||||
Ok(())
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
@ -101,7 +100,7 @@ impl VirtualMachine {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Command::Push(regid) => {
|
Command::Push(regid) => {
|
||||||
if let Some(reg) = &self.registry[regid as usize] {
|
if let Some(reg) = &self.registry[regid] {
|
||||||
if self.stack.len() < usize::MAX {
|
if self.stack.len() < usize::MAX {
|
||||||
self.stack.push(reg.clone());
|
self.stack.push(reg.clone());
|
||||||
//dbg!("Registry pushed", regid, &self.stack);
|
//dbg!("Registry pushed", regid, &self.stack);
|
||||||
@ -114,7 +113,7 @@ impl VirtualMachine {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Command::AssignVariable(heapid, regid) => {
|
Command::AssignVariable(heapid, regid) => {
|
||||||
if let Some(reg) = &self.registry[regid as usize] {
|
if let Some(reg) = &self.registry[regid] {
|
||||||
if let Some(var) = self.heap.get_mut(&heapid) {
|
if let Some(var) = self.heap.get_mut(&heapid) {
|
||||||
var.try_set(Some(reg.clone()))?;
|
var.try_set(Some(reg.clone()))?;
|
||||||
//dbg!("Variable assigned", heapid, regid, &self.heap);
|
//dbg!("Variable assigned", heapid, regid, &self.heap);
|
||||||
@ -129,7 +128,7 @@ impl VirtualMachine {
|
|||||||
Command::VarToReg(heapid, regid) => {
|
Command::VarToReg(heapid, regid) => {
|
||||||
if let Some(var) = self.heap.get(&heapid) {
|
if let Some(var) = self.heap.get(&heapid) {
|
||||||
if let Some(val) = &var.1 {
|
if let Some(val) = &var.1 {
|
||||||
self.registry[regid as usize] = Some(val.clone());
|
self.registry[regid] = Some(val.clone());
|
||||||
//dbg!("Variable pushed to registry", heapid, regid, &self.registry);
|
//dbg!("Variable pushed to registry", heapid, regid, &self.registry);
|
||||||
Ok(())
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
@ -157,10 +156,10 @@ impl VirtualMachine {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Command::FunctionCall(funcid) => {
|
Command::FunctionCall(funcid) => {
|
||||||
if self.functions.len() <= funcid as usize {
|
if self.functions.len() <= funcid {
|
||||||
Err(RuntimePanic::InvalidFuncAddress)
|
Err(RuntimePanic::InvalidFuncAddress)
|
||||||
} else {
|
} else {
|
||||||
match &self.functions[funcid as usize] {
|
match &self.functions[funcid] {
|
||||||
FunctionDef::Builtin(f) => {
|
FunctionDef::Builtin(f) => {
|
||||||
let mut params = Vec::new();
|
let mut params = Vec::new();
|
||||||
for _ in 0..f.signature.parameters.len() {
|
for _ in 0..f.signature.parameters.len() {
|
||||||
@ -198,3 +197,32 @@ 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
Block a user