Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions src/compiler.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::errors::SourceError;
use crate::parser::{AstNode, Block, NodeId};
use crate::parser::{AstNode, Block, Expr, NodeId};
use crate::protocol::Command;
use crate::resolver::{DeclId, Frame, NameBindings, ScopeId, VarId, Variable};
use crate::resolver::{
DeclId, Frame, NameBindings, ScopeId, TypeDecl, TypeDeclId, VarId, Variable,
};
use crate::typechecker::{TypeId, Types};
use std::collections::HashMap;

Expand Down Expand Up @@ -57,8 +59,14 @@ pub struct Compiler {
pub variables: Vec<Variable>,
/// Mapping of variable's name node -> Variable
pub var_resolution: HashMap<NodeId, VarId>,
/// Declarations (commands, aliases, externs), indexed by VarId
/// Type declarations, indexed by TypeDeclId
pub type_decls: Vec<TypeDecl>,
/// Mapping of type decl's name node -> TypeDecl
pub type_resolution: HashMap<NodeId, TypeDeclId>,
/// Declarations (commands, aliases, externs), indexed by DeclId
pub decls: Vec<Box<dyn Command>>,
/// Declaration NodeIds, indexed by DeclId
pub decl_nodes: Vec<NodeId>,
/// Mapping of decl's name node -> Command
pub decl_resolution: HashMap<NodeId, DeclId>,

Expand Down Expand Up @@ -94,7 +102,10 @@ impl Compiler {
scope_stack: vec![],
variables: vec![],
var_resolution: HashMap::new(),
type_decls: vec![],
type_resolution: HashMap::new(),
decls: vec![],
decl_nodes: vec![],
decl_resolution: HashMap::new(),

// variables: vec![],
Expand Down Expand Up @@ -126,7 +137,9 @@ impl Compiler {

if matches!(
ast_node,
AstNode::Name | AstNode::Variable | AstNode::Int | AstNode::Float | AstNode::String
AstNode::Name
| AstNode::VarDecl
| AstNode::Expr(Expr::VarRef | Expr::Int | Expr::Float | Expr::String)
) {
result.push_str(&format!(
" \"{}\"",
Expand Down Expand Up @@ -155,7 +168,10 @@ impl Compiler {
self.scope_stack.extend(name_bindings.scope_stack);
self.variables.extend(name_bindings.variables);
self.var_resolution.extend(name_bindings.var_resolution);
self.type_decls.extend(name_bindings.type_decls);
self.type_resolution.extend(name_bindings.type_resolution);
self.decls.extend(name_bindings.decls);
self.decl_nodes.extend(name_bindings.decl_nodes);
self.decl_resolution.extend(name_bindings.decl_resolution);
self.errors.extend(name_bindings.errors);
}
Expand Down
8 changes: 4 additions & 4 deletions src/ir_generator.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::compiler::Compiler;
use crate::errors::{Severity, SourceError};
use crate::parser::{AstNode, NodeId};
use crate::parser::{AstNode, Expr, NodeId};
use nu_protocol::ast::{Math, Operator};
use nu_protocol::ir::{Instruction, IrBlock, Literal};
use nu_protocol::{RegId, Span};
Expand Down Expand Up @@ -97,7 +97,7 @@ impl<'a> IrGenerator<'a> {
fn generate_node(&mut self, node_id: NodeId) -> Option<RegId> {
let ast_node = &self.compiler.ast_nodes[node_id.0];
match ast_node {
AstNode::Int => {
AstNode::Expr(Expr::Int) => {
let next_reg = self.next_register();
let val = self.compiler.node_as_i64(node_id);
self.add_instruction(
Expand All @@ -109,7 +109,7 @@ impl<'a> IrGenerator<'a> {
);
Some(next_reg)
}
AstNode::Block(block_id) => {
AstNode::Expr(Expr::Block(block_id)) => {
let block = &self.compiler.blocks[block_id.0];
let mut last = None;
for id in &block.nodes {
Expand All @@ -118,7 +118,7 @@ impl<'a> IrGenerator<'a> {
}
last
}
AstNode::BinaryOp { lhs, op, rhs } => {
AstNode::Expr(Expr::BinaryOp { lhs, op, rhs }) => {
let l = self.generate_node(*lhs)?;
let r = self.generate_node(*rhs)?;
let o = self.node_to_operator(*op)?;
Expand Down
Loading
Loading