WIP simplify
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
use super::ops::Op;
|
||||
use super::types::{Dictionary, SourceSpan};
|
||||
use super::words::{compile_word, simple_op};
|
||||
use super::words::compile_word;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
enum Token {
|
||||
@@ -25,6 +25,11 @@ fn tokenize(input: &str) -> Vec<Token> {
|
||||
continue;
|
||||
}
|
||||
|
||||
if c == '(' || c == ')' {
|
||||
chars.next();
|
||||
continue;
|
||||
}
|
||||
|
||||
if c == '"' {
|
||||
let start = pos;
|
||||
chars.next();
|
||||
@@ -88,7 +93,6 @@ fn tokenize(input: &str) -> Vec<Token> {
|
||||
fn compile(tokens: &[Token], dict: &Dictionary) -> Result<Vec<Op>, String> {
|
||||
let mut ops = Vec::new();
|
||||
let mut i = 0;
|
||||
let mut list_depth: usize = 0;
|
||||
|
||||
while i < tokens.len() {
|
||||
match &tokens[i] {
|
||||
@@ -122,20 +126,6 @@ fn compile(tokens: &[Token], dict: &Dictionary) -> Result<Vec<Op>, String> {
|
||||
ops.push(Op::Branch(else_ops.len()));
|
||||
ops.extend(else_ops);
|
||||
}
|
||||
} else if is_list_start(word) {
|
||||
ops.push(Op::ListStart);
|
||||
list_depth += 1;
|
||||
} else if is_list_end(word) {
|
||||
list_depth = list_depth.saturating_sub(1);
|
||||
if let Some(op) = simple_op(word) {
|
||||
ops.push(op);
|
||||
}
|
||||
} else if list_depth > 0 {
|
||||
let mut word_ops = Vec::new();
|
||||
if !compile_word(word, Some(*span), &mut word_ops, dict) {
|
||||
return Err(format!("unknown word: {word}"));
|
||||
}
|
||||
ops.push(Op::Quotation(word_ops, Some(*span)));
|
||||
} else if !compile_word(word, Some(*span), &mut ops, dict) {
|
||||
return Err(format!("unknown word: {word}"));
|
||||
}
|
||||
@@ -147,14 +137,6 @@ fn compile(tokens: &[Token], dict: &Dictionary) -> Result<Vec<Op>, String> {
|
||||
Ok(ops)
|
||||
}
|
||||
|
||||
fn is_list_start(word: &str) -> bool {
|
||||
matches!(word, "[" | "<" | "<<")
|
||||
}
|
||||
|
||||
fn is_list_end(word: &str) -> bool {
|
||||
matches!(word, "]" | ">" | ">>")
|
||||
}
|
||||
|
||||
fn compile_quotation(tokens: &[Token], dict: &Dictionary) -> Result<(Vec<Op>, usize, SourceSpan), String> {
|
||||
let mut depth = 1;
|
||||
let mut end_idx = None;
|
||||
|
||||
Reference in New Issue
Block a user