intern blocks sooner

This commit is contained in:
JT 2021-07-17 08:26:40 +12:00
parent 7922bb4020
commit 949c6a5932
4 changed files with 19 additions and 17 deletions

View file

@ -53,7 +53,7 @@ impl Engine {
val: i,
span: expr.span,
}),
Expr::Var(v) => Err(ShellError::Unsupported(expr.span)),
Expr::Var(_) => Err(ShellError::Unsupported(expr.span)),
Expr::Call(_) => Err(ShellError::Unsupported(expr.span)),
Expr::ExternalCall(_, _) => Err(ShellError::Unsupported(expr.span)),
Expr::Operator(_) => Err(ShellError::Unsupported(expr.span)),

View file

@ -1,4 +1,4 @@
use engine_q::{Engine, ParserWorkingSet, Signature, SyntaxShape};
use engine_q::{ParserWorkingSet, Signature, SyntaxShape};
fn main() -> std::io::Result<()> {
if let Some(path) = std::env::args().nth(1) {
@ -70,7 +70,7 @@ fn main() -> std::io::Result<()> {
println!("{:#?}", output);
println!("error: {:?}", err);
println!("working set: {:#?}", working_set);
//println!("working set: {:#?}", working_set);
// println!("{}", size_of::<Statement>());

View file

@ -4,7 +4,7 @@ use crate::{
lex, lite_parse,
parser_state::{Type, VarId},
signature::Flag,
DeclId, Declaration, LiteBlock, ParseError, ParserWorkingSet, Signature, Span,
BlockId, DeclId, Declaration, LiteBlock, ParseError, ParserWorkingSet, Signature, Span,
};
/// The syntactic shapes that values must match to be passed into a command. You can think of this as the type-checking that occurs when you call a function.
@ -129,8 +129,8 @@ pub enum Expr {
ExternalCall(Vec<u8>, Vec<Vec<u8>>),
Operator(Operator),
BinaryOp(Box<Expression>, Box<Expression>, Box<Expression>), //lhs, op, rhs
Subexpression(Box<Block>),
Block(Box<Block>),
Subexpression(BlockId),
Block(BlockId),
List(Vec<Expression>),
Table(Vec<Expression>, Vec<Vec<Expression>>),
Literal(Vec<u8>),
@ -178,9 +178,9 @@ impl Expression {
}
}
pub fn as_block(self) -> Option<Box<Block>> {
pub fn as_block(self) -> Option<BlockId> {
match self.expr {
Expr::Block(block) => Some(block),
Expr::Block(block_id) => Some(block_id),
_ => None,
}
}
@ -796,9 +796,11 @@ impl ParserWorkingSet {
let (output, err) = self.parse_block(&output);
error = error.or(err);
let block_id = self.add_block(output);
(
Expression {
expr: Expr::Subexpression(Box::new(output)),
expr: Expr::Subexpression(block_id),
span,
},
error,
@ -1082,9 +1084,11 @@ impl ParserWorkingSet {
println!("{:?} {:?}", output, error);
let block_id = self.add_block(output);
(
Expression {
expr: Expr::Block(Box::new(output)),
expr: Expr::Block(block_id),
span,
},
error,
@ -1423,14 +1427,12 @@ impl ParserWorkingSet {
.into_iter()
.map(|x| x.as_var().expect("internal error: expected parameter"))
.collect::<Vec<_>>();
let block = call
let block_id = call
.positional
.remove(0)
.as_block()
.expect("internal error: expected block");
let block_id = self.add_block(block);
let decl = Declaration {
signature: Signature::new(name),
body: Some(block_id),

View file

@ -1,4 +1,4 @@
use crate::{parser::Block, Declaration, Signature, Span};
use crate::{parser::Block, Declaration, Span};
use std::{collections::HashMap, sync::Arc};
#[derive(Debug)]
@ -7,7 +7,7 @@ pub struct ParserState {
file_contents: Vec<u8>,
vars: Vec<Type>,
decls: Vec<Declaration>,
blocks: Vec<Box<Block>>,
blocks: Vec<Block>,
}
#[derive(Clone, Copy, Debug)]
@ -118,7 +118,7 @@ pub struct ParserWorkingSet {
pub(crate) file_contents: Vec<u8>,
vars: Vec<Type>, // indexed by VarId
decls: Vec<Declaration>, // indexed by DeclId
blocks: Vec<Box<Block>>, // indexed by BlockId
blocks: Vec<Block>, // indexed by BlockId
permanent_state: Option<Arc<ParserState>>,
scope: Vec<ScopeFrame>,
}
@ -181,7 +181,7 @@ impl ParserWorkingSet {
decl_id
}
pub fn add_block(&mut self, block: Box<Block>) -> BlockId {
pub fn add_block(&mut self, block: Block) -> BlockId {
self.blocks.push(block);
self.num_blocks() - 1