work out some initial direction for code gen

This commit is contained in:
Kasey White
2022-10-27 03:12:45 -04:00
committed by Lucas
parent 51302f1730
commit ffa78e4c30
9 changed files with 464 additions and 148 deletions

View File

@@ -53,9 +53,9 @@ impl UntypedModule {
pub fn dependencies(&self) -> Vec<(String, Span)> {
self.definitions()
.flat_map(|def| {
if let Definition::Use {
if let Definition::Use(Use {
location, module, ..
} = def
}) = def
{
Some((module.join("/"), *location))
} else {
@@ -69,58 +69,73 @@ impl UntypedModule {
pub type TypedDefinition = Definition<Arc<Type>, TypedExpr, String, String>;
pub type UntypedDefinition = Definition<(), UntypedExpr, (), ()>;
#[derive(Debug, Clone, PartialEq)]
pub struct Function<T, Expr> {
pub arguments: Vec<Arg<T>>,
pub body: Expr,
pub doc: Option<String>,
pub location: Span,
pub name: String,
pub public: bool,
pub return_annotation: Option<Annotation>,
pub return_type: T,
pub end_position: usize,
}
#[derive(Debug, Clone, PartialEq)]
pub struct TypeAlias<T> {
pub alias: String,
pub annotation: Annotation,
pub doc: Option<String>,
pub location: Span,
pub parameters: Vec<String>,
pub public: bool,
pub tipo: T,
}
#[derive(Debug, Clone, PartialEq)]
pub struct DataType<T> {
pub constructors: Vec<RecordConstructor<T>>,
pub doc: Option<String>,
pub location: Span,
pub name: String,
pub opaque: bool,
pub parameters: Vec<String>,
pub public: bool,
pub typed_parameters: Vec<T>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Use<PackageName> {
pub as_name: Option<String>,
pub location: Span,
pub module: Vec<String>,
pub package: PackageName,
pub unqualified: Vec<UnqualifiedImport>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct ModuleConstant<T, ConstantRecordTag> {
pub doc: Option<String>,
pub location: Span,
pub public: bool,
pub name: String,
pub annotation: Option<Annotation>,
pub value: Box<Constant<T, ConstantRecordTag>>,
pub tipo: T,
}
#[derive(Debug, Clone, PartialEq)]
pub enum Definition<T, Expr, ConstantRecordTag, PackageName> {
Fn {
arguments: Vec<Arg<T>>,
body: Expr,
doc: Option<String>,
location: Span,
name: String,
public: bool,
return_annotation: Option<Annotation>,
return_type: T,
end_position: usize,
},
Fn(Function<T, Expr>),
TypeAlias {
alias: String,
annotation: Annotation,
doc: Option<String>,
location: Span,
parameters: Vec<String>,
public: bool,
tipo: T,
},
TypeAlias(TypeAlias<T>),
DataType {
constructors: Vec<RecordConstructor<T>>,
doc: Option<String>,
location: Span,
name: String,
opaque: bool,
parameters: Vec<String>,
public: bool,
typed_parameters: Vec<T>,
},
DataType(DataType<T>),
Use {
as_name: Option<String>,
location: Span,
module: Vec<String>,
package: PackageName,
unqualified: Vec<UnqualifiedImport>,
},
Use(Use<PackageName>),
ModuleConstant {
doc: Option<String>,
location: Span,
public: bool,
name: String,
annotation: Option<Annotation>,
value: Box<Constant<T, ConstantRecordTag>>,
tipo: T,
},
ModuleConstant(ModuleConstant<T, ConstantRecordTag>),
}
impl<A, B, C, E> Definition<A, B, C, E> {

View File

@@ -119,12 +119,14 @@ pub fn import_parser() -> impl Parser<Token, ast::UntypedDefinition, Error = Par
.then(as_name);
just(Token::Use).ignore_then(module_path).map_with_span(
|((module, unqualified), as_name), span| ast::UntypedDefinition::Use {
module,
as_name,
unqualified: unqualified.unwrap_or_default(),
package: (),
location: span,
|((module, unqualified), as_name), span| {
ast::UntypedDefinition::Use(ast::Use {
module,
as_name,
unqualified: unqualified.unwrap_or_default(),
package: (),
location: span,
})
},
)
}
@@ -176,7 +178,7 @@ pub fn data_parser() -> impl Parser<Token, ast::UntypedDefinition, Error = Parse
.then(type_name_with_args())
.then(choice((constructors, record_sugar)))
.map_with_span(|((pub_opaque, (name, parameters)), constructors), span| {
ast::UntypedDefinition::DataType {
ast::UntypedDefinition::DataType(ast::DataType {
location: span,
constructors: constructors
.into_iter()
@@ -196,7 +198,7 @@ pub fn data_parser() -> impl Parser<Token, ast::UntypedDefinition, Error = Parse
parameters: parameters.unwrap_or_default(),
public: pub_opaque.is_some(),
typed_parameters: vec![],
}
})
})
}
@@ -207,7 +209,7 @@ pub fn type_alias_parser() -> impl Parser<Token, ast::UntypedDefinition, Error =
.then_ignore(just(Token::Equal))
.then(type_parser())
.map_with_span(|((opt_pub, (alias, parameters)), annotation), span| {
ast::UntypedDefinition::TypeAlias {
ast::UntypedDefinition::TypeAlias(ast::TypeAlias {
alias,
annotation,
doc: None,
@@ -215,7 +217,7 @@ pub fn type_alias_parser() -> impl Parser<Token, ast::UntypedDefinition, Error =
parameters: parameters.unwrap_or_default(),
public: opt_pub.is_some(),
tipo: (),
}
})
})
}
@@ -239,7 +241,7 @@ pub fn fn_parser() -> impl Parser<Token, ast::UntypedDefinition, Error = ParseEr
)
.map_with_span(
|((((opt_pub, name), (arguments, args_span)), return_annotation), body), span| {
ast::UntypedDefinition::Fn {
ast::UntypedDefinition::Fn(ast::Function {
arguments,
body: body.unwrap_or(expr::UntypedExpr::Todo {
kind: TodoKind::EmptyFunction,
@@ -259,7 +261,7 @@ pub fn fn_parser() -> impl Parser<Token, ast::UntypedDefinition, Error = ParseEr
public: opt_pub.is_some(),
return_annotation,
return_type: (),
}
})
},
)
}

View File

@@ -2,8 +2,9 @@ use chumsky::prelude::*;
use pretty_assertions::assert_eq;
use crate::{
ast::{self, Span},
expr, parser,
ast::{self, DataType, Function, Span, TypeAlias, Use},
expr, lexer,
parser::module_parser,
};
#[test]
@@ -116,14 +117,14 @@ fn module() {
name: "".to_string(),
type_info: (),
definitions: vec![
ast::UntypedDefinition::Use {
ast::UntypedDefinition::Use(Use {
location: Span::new((), 13..25),
module: vec!["std".to_string(), "list".to_string()],
as_name: None,
unqualified: vec![],
package: (),
},
ast::UntypedDefinition::Use {
}),
ast::UntypedDefinition::Use(Use {
location: Span::new((), 38..80),
module: vec!["std".to_string(), "address".to_string()],
as_name: None,
@@ -142,15 +143,15 @@ fn module() {
}
],
package: (),
},
ast::UntypedDefinition::Use {
}),
ast::UntypedDefinition::Use(Use {
location: Span::new((), 93..108),
module: vec!["std".to_string(), "tx".to_string()],
as_name: Some("t".to_string()),
unqualified: vec![],
package: (),
},
ast::UntypedDefinition::DataType {
}),
ast::UntypedDefinition::DataType(DataType {
location: Span::new((), 122..240),
constructors: vec![
ast::RecordConstructor {
@@ -229,8 +230,8 @@ fn module() {
parameters: vec!["a".to_string(),],
public: false,
typed_parameters: vec![],
},
ast::UntypedDefinition::DataType {
}),
ast::UntypedDefinition::DataType(DataType {
location: Span::new((), 254..313),
constructors: vec![ast::RecordConstructor {
location: Span::new((), 275..313),
@@ -254,8 +255,8 @@ fn module() {
parameters: vec![],
public: true,
typed_parameters: vec![],
},
ast::UntypedDefinition::TypeAlias {
}),
ast::UntypedDefinition::TypeAlias(TypeAlias {
alias: "Thing".to_string(),
annotation: ast::Annotation::Constructor {
location: Span::new((), 340..351),
@@ -273,8 +274,8 @@ fn module() {
parameters: vec![],
public: false,
tipo: (),
},
ast::UntypedDefinition::TypeAlias {
}),
ast::UntypedDefinition::TypeAlias(TypeAlias {
alias: "Me".to_string(),
annotation: ast::Annotation::Constructor {
location: Span::new((), 379..393),
@@ -292,8 +293,9 @@ fn module() {
parameters: vec![],
public: true,
tipo: (),
},
ast::UntypedDefinition::Fn {
}),
ast::UntypedDefinition::Fn(Function {
end_position: 0,
arguments: vec![ast::Arg {
arg_name: ast::ArgName::Named {
name: "a".to_string(),
@@ -326,9 +328,8 @@ fn module() {
arguments: vec![],
},),
return_type: (),
end_position: 466,
},
ast::UntypedDefinition::Fn {
}),
ast::UntypedDefinition::Fn(Function {
end_position: 598,
arguments: vec![ast::Arg {
arg_name: ast::ArgName::NamedLabeled {
@@ -375,8 +376,8 @@ fn module() {
public: true,
return_annotation: None,
return_type: (),
},
ast::UntypedDefinition::Fn {
}),
ast::UntypedDefinition::Fn(Function {
end_position: 839,
arguments: vec![ast::Arg {
arg_name: ast::ArgName::Named {
@@ -480,8 +481,8 @@ fn module() {
public: true,
return_annotation: None,
return_type: (),
},
ast::UntypedDefinition::Fn {
}),
ast::UntypedDefinition::Fn(Function {
end_position: 1238,
arguments: vec![ast::Arg {
arg_name: ast::ArgName::Named {
@@ -647,8 +648,8 @@ fn module() {
public: true,
return_annotation: None,
return_type: (),
},
ast::UntypedDefinition::Fn {
}),
ast::UntypedDefinition::Fn(Function {
end_position: 1377,
arguments: vec![],
body: expr::UntypedExpr::Sequence {
@@ -724,8 +725,8 @@ fn module() {
arguments: vec![],
},),
return_type: (),
},
ast::UntypedDefinition::Fn {
}),
ast::UntypedDefinition::Fn(Function {
end_position: 1402,
arguments: vec![],
body: expr::UntypedExpr::Todo {
@@ -739,8 +740,8 @@ fn module() {
public: false,
return_annotation: None,
return_type: (),
},
ast::UntypedDefinition::Fn {
}),
ast::UntypedDefinition::Fn(Function {
end_position: 1477,
arguments: vec![ast::Arg {
arg_name: ast::ArgName::Named {
@@ -770,8 +771,8 @@ fn module() {
public: false,
return_annotation: None,
return_type: (),
},
ast::UntypedDefinition::Fn {
}),
ast::UntypedDefinition::Fn(Function {
end_position: 1655,
arguments: vec![],
body: expr::UntypedExpr::Sequence {
@@ -912,8 +913,8 @@ fn module() {
public: false,
return_annotation: None,
return_type: (),
},
ast::UntypedDefinition::Fn {
}),
ast::UntypedDefinition::Fn(Function {
end_position: 1781,
arguments: vec![
ast::Arg {
@@ -978,8 +979,8 @@ fn module() {
arguments: vec![],
},),
return_type: (),
},
ast::UntypedDefinition::Fn {
}),
ast::UntypedDefinition::Fn(Function {
end_position: 2049,
arguments: vec![],
body: expr::UntypedExpr::If {
@@ -1054,7 +1055,7 @@ fn module() {
public: false,
return_annotation: None,
return_type: (),
},
}),
]
},
);

View File

@@ -8,8 +8,9 @@ use itertools::Itertools;
use crate::{
ast::{
Annotation, ArgName, CallArg, Definition, Pattern, RecordConstructor, RecordConstructorArg,
Span, TypedDefinition, UnqualifiedImport, UntypedDefinition, PIPE_VARIABLE,
Annotation, ArgName, CallArg, DataType, Definition, Function, ModuleConstant, Pattern,
RecordConstructor, RecordConstructorArg, Span, TypeAlias, TypedDefinition,
UnqualifiedImport, UntypedDefinition, Use, PIPE_VARIABLE,
},
builtins::{function, generic_var, unbound_var},
tipo::fields::FieldMap,
@@ -186,7 +187,7 @@ impl<'a> Environment<'a> {
module_name: &String,
) -> TypedDefinition {
match s {
Definition::Fn {
Definition::Fn(Function {
doc,
location,
name,
@@ -196,7 +197,7 @@ impl<'a> Environment<'a> {
return_annotation,
return_type,
end_position,
} => {
}) => {
// Lookup the inferred function information
let function = self
.get_variable(&name)
@@ -230,7 +231,7 @@ impl<'a> Environment<'a> {
},
);
Definition::Fn {
Definition::Fn(Function {
doc,
location,
name,
@@ -240,7 +241,7 @@ impl<'a> Environment<'a> {
return_type,
body,
end_position,
}
})
}
definition @ (Definition::TypeAlias { .. }
@@ -655,13 +656,13 @@ impl<'a> Environment<'a> {
pub fn register_import(&mut self, def: &UntypedDefinition) -> Result<(), Error> {
match def {
Definition::Use {
Definition::Use(Use {
module,
as_name,
unqualified,
location,
..
} => {
}) => {
let name = module.join("/");
// Find imported module
@@ -819,14 +820,14 @@ impl<'a> Environment<'a> {
names: &mut HashMap<&'a str, &'a Span>,
) -> Result<(), Error> {
match def {
Definition::DataType {
Definition::DataType(DataType {
name,
public,
parameters,
location,
constructors,
..
} => {
}) => {
assert_unique_type_name(names, name, location)?;
// Build a type from the type Annotation
@@ -864,14 +865,14 @@ impl<'a> Environment<'a> {
}
}
Definition::TypeAlias {
Definition::TypeAlias(TypeAlias {
location,
public,
parameters: args,
alias: name,
annotation: resolved_type,
..
} => {
}) => {
assert_unique_type_name(names, name, location)?;
// Register the paramerterised types
@@ -915,14 +916,14 @@ impl<'a> Environment<'a> {
names: &mut HashMap<&'a str, &'a Span>,
) -> Result<(), Error> {
match def {
Definition::Fn {
Definition::Fn(Function {
name,
arguments: args,
location,
return_annotation,
public,
..
} => {
}) => {
assert_unique_value_name(names, name, location)?;
self.ungeneralised_functions.insert(name.to_string());
@@ -980,14 +981,14 @@ impl<'a> Environment<'a> {
}
}
Definition::DataType {
Definition::DataType(DataType {
location,
public,
opaque,
name,
constructors,
..
} => {
}) => {
let mut hydrator = hydrators
.remove(name)
.expect("Could not find hydrator for register_values custom type");
@@ -1079,7 +1080,7 @@ impl<'a> Environment<'a> {
}
}
Definition::ModuleConstant { name, location, .. } => {
Definition::ModuleConstant(ModuleConstant { name, location, .. }) => {
assert_unique_const_name(names, name, location)?;
}

View File

@@ -2,8 +2,9 @@ use std::collections::HashMap;
use crate::{
ast::{
Definition, Layer, ModuleKind, RecordConstructor, RecordConstructorArg, TypedDefinition,
TypedModule, UntypedDefinition, UntypedModule,
DataType, Definition, Function, Layer, ModuleConstant, ModuleKind, RecordConstructor,
RecordConstructorArg, TypeAlias, TypedDefinition, TypedModule, UntypedDefinition,
UntypedModule, Use,
},
builtins::function,
parser::token::Token,
@@ -142,7 +143,7 @@ fn infer_definition(
environment: &mut Environment<'_>,
) -> Result<TypedDefinition, Error> {
match def {
Definition::Fn {
Definition::Fn(Function {
doc,
location,
name,
@@ -152,7 +153,7 @@ fn infer_definition(
return_annotation,
end_position,
..
} => {
}) => {
let preregistered_fn = environment
.get_variable(&name)
.expect("Could not find preregistered type for function");
@@ -217,7 +218,7 @@ fn infer_definition(
tipo
};
Ok(Definition::Fn {
Ok(Definition::Fn(Function {
doc,
location,
name,
@@ -229,10 +230,10 @@ fn infer_definition(
.expect("Could not find return type for fn"),
body,
end_position,
})
}))
}
Definition::TypeAlias {
Definition::TypeAlias(TypeAlias {
doc,
location,
public,
@@ -240,14 +241,14 @@ fn infer_definition(
parameters,
annotation,
..
} => {
}) => {
let tipo = environment
.get_type_constructor(&None, &alias, location)
.expect("Could not find existing type for type alias")
.tipo
.clone();
Ok(Definition::TypeAlias {
Ok(Definition::TypeAlias(TypeAlias {
doc,
location,
public,
@@ -255,10 +256,10 @@ fn infer_definition(
parameters,
annotation,
tipo,
})
}))
}
Definition::DataType {
Definition::DataType(DataType {
doc,
location,
public,
@@ -267,7 +268,7 @@ fn infer_definition(
parameters,
constructors,
..
} => {
}) => {
let constructors = constructors
.into_iter()
.map(
@@ -330,7 +331,7 @@ fn infer_definition(
.parameters
.clone();
Ok(Definition::DataType {
Ok(Definition::DataType(DataType {
doc,
location,
public,
@@ -339,16 +340,16 @@ fn infer_definition(
parameters,
constructors,
typed_parameters,
})
}))
}
Definition::Use {
Definition::Use(Use {
location,
module,
as_name,
mut unqualified,
..
} => {
}) => {
let name = module.join("/");
// Find imported module
@@ -371,16 +372,16 @@ fn infer_definition(
}
}
Ok(Definition::Use {
Ok(Definition::Use(Use {
location,
module,
as_name,
unqualified,
package: module_info.package.clone(),
})
}))
}
Definition::ModuleConstant {
Definition::ModuleConstant(ModuleConstant {
doc,
location,
name,
@@ -388,7 +389,7 @@ fn infer_definition(
public,
value,
..
} => {
}) => {
let typed_expr = ExprTyper::new(environment).infer_const(&annotation, *value)?;
let tipo = typed_expr.tipo();
@@ -411,7 +412,7 @@ fn infer_definition(
environment.init_usage(name.clone(), EntityKind::PrivateConstant, location);
}
Ok(Definition::ModuleConstant {
Ok(Definition::ModuleConstant(ModuleConstant {
doc,
location,
name,
@@ -419,7 +420,7 @@ fn infer_definition(
public,
value: Box::new(typed_expr),
tipo,
})
}))
}
}
}