Skip to content

PoC: Generics and better type inference #58

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 14 commits into
base: main
Choose a base branch
from
Draft
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
21 changes: 16 additions & 5 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, Pipeline};
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 @@ -58,8 +60,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 All @@ -71,7 +79,6 @@ pub struct Compiler {

// Use/def
// pub call_resolution: HashMap<NodeId, CallTarget>,
// pub type_resolution: HashMap<NodeId, TypeId>,
pub errors: Vec<SourceError>,
}

Expand All @@ -96,16 +103,17 @@ 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![],
// functions: vec![],
// types: vec![],

// call_resolution: HashMap::new(),
// var_resolution: HashMap::new(),
// type_resolution: HashMap::new(),
errors: vec![],
}
}
Expand Down Expand Up @@ -157,7 +165,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
34 changes: 34 additions & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ pub enum AstNode {
// Definitions
Def {
name: NodeId,
type_params: Option<NodeId>,
params: NodeId,
in_out_types: Option<NodeId>,
block: NodeId,
Expand Down Expand Up @@ -1018,6 +1019,32 @@ impl Parser {
self.create_node(AstNode::Params(param_list), span_start, span_end)
}

pub fn type_params(&mut self) -> NodeId {
let _span = span!();
let span_start = self.position();
self.less_than();

let mut param_list = vec![];

while self.has_tokens() {
if self.is_greater_than() {
break;
}

if self.is_comma() {
self.tokens.advance();
continue;
}

param_list.push(self.name());
}

let span_end = self.position() + 1;
self.greater_than();

self.create_node(AstNode::Params(param_list), span_start, span_end)
}

pub fn type_args(&mut self) -> NodeId {
let _span = span!();
let span_start = self.position();
Expand Down Expand Up @@ -1159,6 +1186,12 @@ impl Parser {
_ => return self.error("expected def name"),
};

let type_params = if self.is_less_than() {
Some(self.type_params())
} else {
None
};

let params = self.signature_params(ParamsContext::Squares);
let in_out_types = if self.is_colon() {
Some(self.in_out_types())
Expand All @@ -1172,6 +1205,7 @@ impl Parser {
self.create_node(
AstNode::Def {
name,
type_params,
params,
in_out_types,
block,
Expand Down
Loading
Loading