further core improvements
This commit is contained in:
parent
7d7782df0c
commit
f76e03c5e6
3 changed files with 36 additions and 17 deletions
30
src/lexer.rs
30
src/lexer.rs
|
@ -11,15 +11,19 @@ pub fn lex(input: String, filename: String) -> Words {
|
||||||
|
|
||||||
fn read_block(str_words: &[String], isfn: bool, origin: &FrameInfo) -> (Option<u32>, Words, usize) {
|
fn read_block(str_words: &[String], isfn: bool, origin: &FrameInfo) -> (Option<u32>, Words, usize) {
|
||||||
let mut rem = None;
|
let mut rem = None;
|
||||||
if str_words[0] == "{" && isfn {
|
|
||||||
let mut r = 0_u32;
|
|
||||||
while str_words[r as usize + 1] != "|" {
|
|
||||||
r += 1;
|
|
||||||
}
|
|
||||||
rem = Some(r);
|
|
||||||
}
|
|
||||||
let mut words = Vec::new();
|
let mut words = Vec::new();
|
||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
|
if str_words[0] == "{" {
|
||||||
|
if isfn {
|
||||||
|
let mut r = 0_u32;
|
||||||
|
while str_words[r as usize + 1] != "|" {
|
||||||
|
r += 1;
|
||||||
|
}
|
||||||
|
i += r as usize + 1;
|
||||||
|
rem = Some(r);
|
||||||
|
}
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
while i < str_words.len() {
|
while i < str_words.len() {
|
||||||
let word = str_words[i].to_owned();
|
let word = str_words[i].to_owned();
|
||||||
match word.as_str() {
|
match word.as_str() {
|
||||||
|
@ -118,6 +122,9 @@ fn read_block(str_words: &[String], isfn: bool, origin: &FrameInfo) -> (Option<u
|
||||||
"}" => {
|
"}" => {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
x if x.starts_with("\"") => {
|
||||||
|
words.push(Word::Const(Constant::Str(x[1..].to_owned())));
|
||||||
|
}
|
||||||
mut x => {
|
mut x => {
|
||||||
let mut ra = 0;
|
let mut ra = 0;
|
||||||
while x.starts_with("&") {
|
while x.starts_with("&") {
|
||||||
|
@ -158,17 +165,15 @@ fn parse_line(line: &str) -> Vec<String> {
|
||||||
if escaping {
|
if escaping {
|
||||||
if c == '\\' {
|
if c == '\\' {
|
||||||
s += "\\";
|
s += "\\";
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
if c == 'n' {
|
if c == 'n' {
|
||||||
s += "\n";
|
s += "\n";
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
if c == 'r' {
|
if c == 'r' {
|
||||||
s += "\r";
|
s += "\r";
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
escaping = false;
|
escaping = false;
|
||||||
|
continue;
|
||||||
} else if c == '"' {
|
} else if c == '"' {
|
||||||
in_string = false;
|
in_string = false;
|
||||||
escaping = false;
|
escaping = false;
|
||||||
|
@ -176,9 +181,11 @@ fn parse_line(line: &str) -> Vec<String> {
|
||||||
}
|
}
|
||||||
if c == '\\' {
|
if c == '\\' {
|
||||||
escaping = true;
|
escaping = true;
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if c == '"' {
|
if c == '"' {
|
||||||
|
s += "\"";
|
||||||
in_string = true;
|
in_string = true;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -193,5 +200,8 @@ fn parse_line(line: &str) -> Vec<String> {
|
||||||
}
|
}
|
||||||
s += String::from(c).as_str();
|
s += String::from(c).as_str();
|
||||||
}
|
}
|
||||||
|
if s != "" {
|
||||||
|
words.push(s);
|
||||||
|
}
|
||||||
words
|
words
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use spl::runtime::*;
|
use spl::{lexer::lex, runtime::*};
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
io::{stdout, Write},
|
io::{stdout, Write},
|
||||||
|
@ -49,5 +49,6 @@ fn main() {
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
.exec(&mut stack);
|
.exec(&mut stack);
|
||||||
|
lex("func println { | print \"\\n\" print } def helloworld \"Hello, World\" =helloworld helloworld println".to_owned(), "TEST".to_owned()).exec(&mut stack);
|
||||||
Runtime::reset();
|
Runtime::reset();
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,6 +35,8 @@ impl Runtime {
|
||||||
rt.make_type("int".to_owned(), |t| t);
|
rt.make_type("int".to_owned(), |t| t);
|
||||||
rt.make_type("long".to_owned(), |t| t);
|
rt.make_type("long".to_owned(), |t| t);
|
||||||
rt.make_type("mega".to_owned(), |t| t);
|
rt.make_type("mega".to_owned(), |t| t);
|
||||||
|
rt.make_type("float".to_owned(), |t| t);
|
||||||
|
rt.make_type("double".to_owned(), |t| t);
|
||||||
rt.make_type("func".to_owned(), |t| t);
|
rt.make_type("func".to_owned(), |t| t);
|
||||||
rt.make_type("array".to_owned(), |t| t);
|
rt.make_type("array".to_owned(), |t| t);
|
||||||
rt.make_type("str".to_owned(), |t| t);
|
rt.make_type("str".to_owned(), |t| t);
|
||||||
|
@ -260,7 +262,7 @@ impl Stack {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone, Debug)]
|
||||||
pub enum Keyword {
|
pub enum Keyword {
|
||||||
/// <none>
|
/// <none>
|
||||||
///
|
///
|
||||||
|
@ -316,11 +318,13 @@ pub enum Constant {
|
||||||
Int(i32),
|
Int(i32),
|
||||||
Long(i64),
|
Long(i64),
|
||||||
Mega(i128),
|
Mega(i128),
|
||||||
|
Float(f32),
|
||||||
|
Double(f64),
|
||||||
Func(AFunc),
|
Func(AFunc),
|
||||||
Str(String),
|
Str(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone, Debug)]
|
||||||
pub enum Word {
|
pub enum Word {
|
||||||
Key(Keyword),
|
Key(Keyword),
|
||||||
Const(Constant),
|
Const(Constant),
|
||||||
|
@ -328,7 +332,7 @@ pub enum Word {
|
||||||
ObjCall(String, bool, u32),
|
ObjCall(String, bool, u32),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct Words {
|
pub struct Words {
|
||||||
pub words: Vec<Word>,
|
pub words: Vec<Word>,
|
||||||
}
|
}
|
||||||
|
@ -433,6 +437,8 @@ impl Object {
|
||||||
Constant::Int(x) => x > &0,
|
Constant::Int(x) => x > &0,
|
||||||
Constant::Long(x) => x > &0,
|
Constant::Long(x) => x > &0,
|
||||||
Constant::Mega(x) => x > &0,
|
Constant::Mega(x) => x > &0,
|
||||||
|
Constant::Float(_) => true,
|
||||||
|
Constant::Double(_) => true,
|
||||||
Constant::Func(_) => true,
|
Constant::Func(_) => true,
|
||||||
Constant::Str(x) => x == "",
|
Constant::Str(x) => x == "",
|
||||||
}
|
}
|
||||||
|
@ -450,9 +456,11 @@ impl From<Constant> for Object {
|
||||||
Constant::Int(_) => x.get_type_by_id(1),
|
Constant::Int(_) => x.get_type_by_id(1),
|
||||||
Constant::Long(_) => x.get_type_by_id(2),
|
Constant::Long(_) => x.get_type_by_id(2),
|
||||||
Constant::Mega(_) => x.get_type_by_id(3),
|
Constant::Mega(_) => x.get_type_by_id(3),
|
||||||
Constant::Func(_) => x.get_type_by_id(4),
|
Constant::Float(_) => x.get_type_by_id(4),
|
||||||
// array is 5
|
Constant::Double(_) => x.get_type_by_id(5),
|
||||||
Constant::Str(_) => x.get_type_by_id(6),
|
Constant::Func(_) => x.get_type_by_id(6),
|
||||||
|
// array is 7
|
||||||
|
Constant::Str(_) => x.get_type_by_id(8),
|
||||||
}
|
}
|
||||||
.expect("runtime uninitialized: default types not set.")
|
.expect("runtime uninitialized: default types not set.")
|
||||||
}),
|
}),
|
||||||
|
|
Loading…
Add table
Reference in a new issue