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, val: i,
span: expr.span, 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::Call(_) => Err(ShellError::Unsupported(expr.span)),
Expr::ExternalCall(_, _) => Err(ShellError::Unsupported(expr.span)), Expr::ExternalCall(_, _) => Err(ShellError::Unsupported(expr.span)),
Expr::Operator(_) => 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<()> { fn main() -> std::io::Result<()> {
if let Some(path) = std::env::args().nth(1) { if let Some(path) = std::env::args().nth(1) {
@ -70,7 +70,7 @@ fn main() -> std::io::Result<()> {
println!("{:#?}", output); println!("{:#?}", output);
println!("error: {:?}", err); println!("error: {:?}", err);
println!("working set: {:#?}", working_set); //println!("working set: {:#?}", working_set);
// println!("{}", size_of::<Statement>()); // println!("{}", size_of::<Statement>());

View file

@ -4,7 +4,7 @@ use crate::{
lex, lite_parse, lex, lite_parse,
parser_state::{Type, VarId}, parser_state::{Type, VarId},
signature::Flag, 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. /// 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>>), ExternalCall(Vec<u8>, Vec<Vec<u8>>),
Operator(Operator), Operator(Operator),
BinaryOp(Box<Expression>, Box<Expression>, Box<Expression>), //lhs, op, rhs BinaryOp(Box<Expression>, Box<Expression>, Box<Expression>), //lhs, op, rhs
Subexpression(Box<Block>), Subexpression(BlockId),
Block(Box<Block>), Block(BlockId),
List(Vec<Expression>), List(Vec<Expression>),
Table(Vec<Expression>, Vec<Vec<Expression>>), Table(Vec<Expression>, Vec<Vec<Expression>>),
Literal(Vec<u8>), 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 { match self.expr {
Expr::Block(block) => Some(block), Expr::Block(block_id) => Some(block_id),
_ => None, _ => None,
} }
} }
@ -796,9 +796,11 @@ impl ParserWorkingSet {
let (output, err) = self.parse_block(&output); let (output, err) = self.parse_block(&output);
error = error.or(err); error = error.or(err);
let block_id = self.add_block(output);
( (
Expression { Expression {
expr: Expr::Subexpression(Box::new(output)), expr: Expr::Subexpression(block_id),
span, span,
}, },
error, error,
@ -1082,9 +1084,11 @@ impl ParserWorkingSet {
println!("{:?} {:?}", output, error); println!("{:?} {:?}", output, error);
let block_id = self.add_block(output);
( (
Expression { Expression {
expr: Expr::Block(Box::new(output)), expr: Expr::Block(block_id),
span, span,
}, },
error, error,
@ -1423,14 +1427,12 @@ impl ParserWorkingSet {
.into_iter() .into_iter()
.map(|x| x.as_var().expect("internal error: expected parameter")) .map(|x| x.as_var().expect("internal error: expected parameter"))
.collect::<Vec<_>>(); .collect::<Vec<_>>();
let block = call let block_id = call
.positional .positional
.remove(0) .remove(0)
.as_block() .as_block()
.expect("internal error: expected block"); .expect("internal error: expected block");
let block_id = self.add_block(block);
let decl = Declaration { let decl = Declaration {
signature: Signature::new(name), signature: Signature::new(name),
body: Some(block_id), 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}; use std::{collections::HashMap, sync::Arc};
#[derive(Debug)] #[derive(Debug)]
@ -7,7 +7,7 @@ pub struct ParserState {
file_contents: Vec<u8>, file_contents: Vec<u8>,
vars: Vec<Type>, vars: Vec<Type>,
decls: Vec<Declaration>, decls: Vec<Declaration>,
blocks: Vec<Box<Block>>, blocks: Vec<Block>,
} }
#[derive(Clone, Copy, Debug)] #[derive(Clone, Copy, Debug)]
@ -118,7 +118,7 @@ pub struct ParserWorkingSet {
pub(crate) file_contents: Vec<u8>, pub(crate) file_contents: Vec<u8>,
vars: Vec<Type>, // indexed by VarId vars: Vec<Type>, // indexed by VarId
decls: Vec<Declaration>, // indexed by DeclId decls: Vec<Declaration>, // indexed by DeclId
blocks: Vec<Box<Block>>, // indexed by BlockId blocks: Vec<Block>, // indexed by BlockId
permanent_state: Option<Arc<ParserState>>, permanent_state: Option<Arc<ParserState>>,
scope: Vec<ScopeFrame>, scope: Vec<ScopeFrame>,
} }
@ -181,7 +181,7 @@ impl ParserWorkingSet {
decl_id 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.blocks.push(block);
self.num_blocks() - 1 self.num_blocks() - 1