mirror of
https://github.com/edg-l/edlang.git
synced 2024-11-22 07:58:24 +00:00
spans
This commit is contained in:
parent
024a06defa
commit
64dd61be3a
246
out
246
out
|
@ -1,246 +0,0 @@
|
|||
Program {
|
||||
statements: [
|
||||
Struct(
|
||||
Struct {
|
||||
name: "Hello",
|
||||
fields: [
|
||||
StructField {
|
||||
ident: "x",
|
||||
type_exp: Integer {
|
||||
bits: 32,
|
||||
signed: true,
|
||||
},
|
||||
},
|
||||
StructField {
|
||||
ident: "y",
|
||||
type_exp: Integer {
|
||||
bits: 32,
|
||||
signed: true,
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
),
|
||||
Function(
|
||||
Function {
|
||||
name: "test",
|
||||
params: [
|
||||
Parameter {
|
||||
ident: "x",
|
||||
type_exp: Other {
|
||||
id: "Hello",
|
||||
},
|
||||
},
|
||||
],
|
||||
body: [
|
||||
Return(
|
||||
None,
|
||||
),
|
||||
],
|
||||
return_type: None,
|
||||
},
|
||||
),
|
||||
Function(
|
||||
Function {
|
||||
name: "works",
|
||||
params: [
|
||||
Parameter {
|
||||
ident: "x",
|
||||
type_exp: Integer {
|
||||
bits: 64,
|
||||
signed: true,
|
||||
},
|
||||
},
|
||||
],
|
||||
body: [
|
||||
Let {
|
||||
name: "z",
|
||||
value: Literal(
|
||||
Integer {
|
||||
value: "0",
|
||||
bits: None,
|
||||
signed: None,
|
||||
},
|
||||
),
|
||||
value_type: None,
|
||||
span: (
|
||||
107,
|
||||
117,
|
||||
),
|
||||
},
|
||||
If {
|
||||
condition: BinaryOp(
|
||||
Literal(
|
||||
Integer {
|
||||
value: "2",
|
||||
bits: None,
|
||||
signed: None,
|
||||
},
|
||||
),
|
||||
Eq,
|
||||
Variable {
|
||||
name: Spanned {
|
||||
span: (
|
||||
130,
|
||||
131,
|
||||
),
|
||||
value: "x",
|
||||
},
|
||||
value_type: None,
|
||||
},
|
||||
),
|
||||
body: [
|
||||
Mutate {
|
||||
name: "z",
|
||||
value: BinaryOp(
|
||||
Variable {
|
||||
name: Spanned {
|
||||
span: (
|
||||
146,
|
||||
147,
|
||||
),
|
||||
value: "x",
|
||||
},
|
||||
value_type: None,
|
||||
},
|
||||
Mul,
|
||||
Literal(
|
||||
Integer {
|
||||
value: "2",
|
||||
bits: None,
|
||||
signed: None,
|
||||
},
|
||||
),
|
||||
),
|
||||
value_type: None,
|
||||
span: (
|
||||
142,
|
||||
152,
|
||||
),
|
||||
},
|
||||
],
|
||||
else_body: Some(
|
||||
[
|
||||
Mutate {
|
||||
name: "z",
|
||||
value: BinaryOp(
|
||||
Variable {
|
||||
name: Spanned {
|
||||
span: (
|
||||
178,
|
||||
179,
|
||||
),
|
||||
value: "x",
|
||||
},
|
||||
value_type: None,
|
||||
},
|
||||
Mul,
|
||||
Literal(
|
||||
Integer {
|
||||
value: "3",
|
||||
bits: None,
|
||||
signed: None,
|
||||
},
|
||||
),
|
||||
),
|
||||
value_type: None,
|
||||
span: (
|
||||
174,
|
||||
184,
|
||||
),
|
||||
},
|
||||
],
|
||||
),
|
||||
},
|
||||
Return(
|
||||
Some(
|
||||
Variable {
|
||||
name: Spanned {
|
||||
span: (
|
||||
202,
|
||||
203,
|
||||
),
|
||||
value: "z",
|
||||
},
|
||||
value_type: None,
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
return_type: Some(
|
||||
Integer {
|
||||
bits: 64,
|
||||
signed: true,
|
||||
},
|
||||
),
|
||||
},
|
||||
),
|
||||
Function(
|
||||
Function {
|
||||
name: "main",
|
||||
params: [],
|
||||
body: [
|
||||
Let {
|
||||
name: "y",
|
||||
value: Literal(
|
||||
Integer {
|
||||
value: "2",
|
||||
bits: None,
|
||||
signed: None,
|
||||
},
|
||||
),
|
||||
value_type: None,
|
||||
span: (
|
||||
231,
|
||||
241,
|
||||
),
|
||||
},
|
||||
Let {
|
||||
name: "z",
|
||||
value: Variable {
|
||||
name: Spanned {
|
||||
span: (
|
||||
254,
|
||||
255,
|
||||
),
|
||||
value: "y",
|
||||
},
|
||||
value_type: None,
|
||||
},
|
||||
value_type: None,
|
||||
span: (
|
||||
246,
|
||||
256,
|
||||
),
|
||||
},
|
||||
Return(
|
||||
Some(
|
||||
Call {
|
||||
function: "works",
|
||||
args: [
|
||||
Variable {
|
||||
name: Spanned {
|
||||
span: (
|
||||
274,
|
||||
275,
|
||||
),
|
||||
value: "z",
|
||||
},
|
||||
value_type: None,
|
||||
},
|
||||
],
|
||||
value_type: None,
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
return_type: Some(
|
||||
Integer {
|
||||
bits: 64,
|
||||
signed: true,
|
||||
},
|
||||
),
|
||||
},
|
||||
),
|
||||
],
|
||||
}
|
|
@ -43,11 +43,21 @@ impl OpCode {
|
|||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub enum TypeExp {
|
||||
Integer { bits: u32, signed: bool },
|
||||
Integer {
|
||||
bits: u32,
|
||||
signed: bool,
|
||||
},
|
||||
Boolean,
|
||||
Array { of: Box<Self>, len: Option<u32> },
|
||||
Pointer { target: Box<Self> },
|
||||
Other { id: String },
|
||||
Array {
|
||||
of: Spanned<Box<Self>>,
|
||||
len: Option<u32>,
|
||||
},
|
||||
Pointer {
|
||||
target: Spanned<Box<Self>>,
|
||||
},
|
||||
Other {
|
||||
id: String,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
|
@ -65,42 +75,42 @@ pub enum LiteralValue {
|
|||
pub enum Expression {
|
||||
Literal(LiteralValue),
|
||||
Variable {
|
||||
name: Spanned<String>,
|
||||
name: String,
|
||||
},
|
||||
Call {
|
||||
function: String,
|
||||
args: Vec<Box<Self>>,
|
||||
function: Spanned<String>,
|
||||
args: Vec<Spanned<Box<Self>>>,
|
||||
},
|
||||
BinaryOp(Box<Self>, OpCode, Box<Self>),
|
||||
BinaryOp(Spanned<Box<Self>>, OpCode, Spanned<Box<Self>>),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub struct Parameter {
|
||||
pub ident: String,
|
||||
pub type_exp: TypeExp,
|
||||
pub ident: Spanned<String>,
|
||||
pub type_exp: Spanned<TypeExp>,
|
||||
}
|
||||
|
||||
impl Parameter {
|
||||
pub const fn new(ident: String, type_exp: TypeExp) -> Self {
|
||||
pub const fn new(ident: Spanned<String>, type_exp: Spanned<TypeExp>) -> Self {
|
||||
Self { ident, type_exp }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Function {
|
||||
pub name: String,
|
||||
pub name: Spanned<String>,
|
||||
pub params: Vec<Parameter>,
|
||||
pub body: Vec<Statement>,
|
||||
pub body: Vec<Spanned<Statement>>,
|
||||
pub scope_type_info: HashMap<String, Vec<TypeExp>>,
|
||||
pub return_type: Option<TypeExp>,
|
||||
pub return_type: Option<Spanned<TypeExp>>,
|
||||
}
|
||||
|
||||
impl Function {
|
||||
pub fn new(
|
||||
name: String,
|
||||
name: Spanned<String>,
|
||||
params: Vec<Parameter>,
|
||||
body: Vec<Statement>,
|
||||
return_type: Option<TypeExp>,
|
||||
body: Vec<Spanned<Statement>>,
|
||||
return_type: Option<Spanned<TypeExp>>,
|
||||
) -> Self {
|
||||
Self {
|
||||
name,
|
||||
|
@ -114,12 +124,12 @@ impl Function {
|
|||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub struct StructField {
|
||||
pub ident: String,
|
||||
pub field_type: TypeExp,
|
||||
pub ident: Spanned<String>,
|
||||
pub field_type: Spanned<TypeExp>,
|
||||
}
|
||||
|
||||
impl StructField {
|
||||
pub const fn new(ident: String, type_name: TypeExp) -> Self {
|
||||
pub const fn new(ident: Spanned<String>, type_name: Spanned<TypeExp>) -> Self {
|
||||
Self {
|
||||
ident,
|
||||
field_type: type_name,
|
||||
|
@ -129,42 +139,40 @@ impl StructField {
|
|||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub struct Struct {
|
||||
pub name: String,
|
||||
pub name: Spanned<String>,
|
||||
pub fields: Vec<StructField>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum Statement {
|
||||
Let {
|
||||
name: String,
|
||||
value: Box<Expression>,
|
||||
value_type: Option<TypeExp>,
|
||||
span: (usize, usize),
|
||||
name: Spanned<String>,
|
||||
value: Spanned<Box<Expression>>,
|
||||
value_type: Option<Spanned<TypeExp>>,
|
||||
},
|
||||
Mutate {
|
||||
name: String,
|
||||
value: Box<Expression>,
|
||||
span: (usize, usize),
|
||||
name: Spanned<String>,
|
||||
value: Spanned<Box<Expression>>,
|
||||
},
|
||||
If {
|
||||
condition: Box<Expression>,
|
||||
body: Vec<Statement>,
|
||||
condition: Spanned<Box<Expression>>,
|
||||
body: Vec<Spanned<Statement>>,
|
||||
scope_type_info: HashMap<String, Vec<TypeExp>>,
|
||||
else_body: Option<Vec<Statement>>,
|
||||
else_body: Option<Vec<Spanned<Statement>>>,
|
||||
else_body_scope_type_info: HashMap<String, Vec<TypeExp>>,
|
||||
},
|
||||
Return(Option<Box<Expression>>),
|
||||
Return(Option<Spanned<Box<Expression>>>),
|
||||
Function(Function),
|
||||
Struct(Struct),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Program {
|
||||
pub statements: Vec<Statement>,
|
||||
pub statements: Vec<Spanned<Statement>>,
|
||||
}
|
||||
|
||||
impl Program {
|
||||
pub fn new(statements: Vec<Statement>) -> Self {
|
||||
pub fn new(statements: Vec<Spanned<Statement>>) -> Self {
|
||||
Self { statements }
|
||||
}
|
||||
}
|
||||
|
|
31
src/check.rs
31
src/check.rs
|
@ -22,8 +22,8 @@ pub fn check<'a>(data: &'a ProgramData, ast: &ast::Program) -> Vec<Check<'a>> {
|
|||
let mut errors = vec![];
|
||||
|
||||
for statement in &ast.statements {
|
||||
match &statement {
|
||||
Statement::Let { name: _, span, .. } => {
|
||||
match &statement.value {
|
||||
Statement::Let { name, .. } => {
|
||||
// can't have a top level assignment yet.
|
||||
let snippet = Snippet {
|
||||
title: Some(Annotation {
|
||||
|
@ -40,7 +40,7 @@ pub fn check<'a>(data: &'a ProgramData, ast: &ast::Program) -> Vec<Check<'a>> {
|
|||
annotations: vec![SourceAnnotation {
|
||||
label: "unexpected statement",
|
||||
annotation_type: AnnotationType::Error,
|
||||
range: *span,
|
||||
range: name.span,
|
||||
}],
|
||||
}],
|
||||
opt: FormatOptions {
|
||||
|
@ -52,7 +52,7 @@ pub fn check<'a>(data: &'a ProgramData, ast: &ast::Program) -> Vec<Check<'a>> {
|
|||
let dl = DisplayList::from(snippet);
|
||||
errors.push(Check::Error(dl));
|
||||
}
|
||||
Statement::Mutate { span, .. } => {
|
||||
Statement::Mutate { name, .. } => {
|
||||
// can't have a top level assignment yet.
|
||||
let snippet = Snippet {
|
||||
title: Some(Annotation {
|
||||
|
@ -69,7 +69,7 @@ pub fn check<'a>(data: &'a ProgramData, ast: &ast::Program) -> Vec<Check<'a>> {
|
|||
annotations: vec![SourceAnnotation {
|
||||
label: "unexpected statement",
|
||||
annotation_type: AnnotationType::Error,
|
||||
range: *span,
|
||||
range: name.span,
|
||||
}],
|
||||
}],
|
||||
opt: FormatOptions {
|
||||
|
@ -112,9 +112,15 @@ pub fn print_error(source: &str, err: ParseError<usize, Token, LexicalError>) {
|
|||
let dl = DisplayList::from(snippet);
|
||||
println!("{dl}");
|
||||
}
|
||||
ParseError::UnrecognizedEof { location, expected } => todo!(),
|
||||
ParseError::UnrecognizedToken { token, expected } => todo!(),
|
||||
ParseError::ExtraToken { token } => todo!(),
|
||||
ParseError::UnrecognizedEof {
|
||||
location: _,
|
||||
expected: _,
|
||||
} => todo!(),
|
||||
ParseError::UnrecognizedToken {
|
||||
token: _,
|
||||
expected: _,
|
||||
} => todo!(),
|
||||
ParseError::ExtraToken { token: _ } => todo!(),
|
||||
ParseError::User { error } => match error {
|
||||
LexicalError::InvalidToken(err, range) => {
|
||||
let title = format!("invalid token (lexical error): {:?}", err);
|
||||
|
@ -126,7 +132,7 @@ pub fn print_error(source: &str, err: ParseError<usize, Token, LexicalError>) {
|
|||
}),
|
||||
footer: vec![],
|
||||
slices: vec![Slice {
|
||||
source: source,
|
||||
source,
|
||||
line_start: 1,
|
||||
fold: false,
|
||||
origin: None,
|
||||
|
@ -149,11 +155,10 @@ pub fn print_error(source: &str, err: ParseError<usize, Token, LexicalError>) {
|
|||
}
|
||||
|
||||
pub fn print_type_error(source: &str, err: TypeError) {
|
||||
dbg!(&err);
|
||||
match err {
|
||||
TypeError::Mismatch {
|
||||
found,
|
||||
expected,
|
||||
found: _,
|
||||
expected: _,
|
||||
span,
|
||||
} => {
|
||||
let snippet = Snippet {
|
||||
|
@ -182,7 +187,7 @@ pub fn print_type_error(source: &str, err: TypeError) {
|
|||
let dl = DisplayList::from(snippet);
|
||||
println!("{dl}");
|
||||
}
|
||||
TypeError::UndeclaredVariable { name, span } => {
|
||||
TypeError::UndeclaredVariable { name: _, span } => {
|
||||
let snippet = Snippet {
|
||||
title: Some(Annotation {
|
||||
id: None,
|
||||
|
|
|
@ -16,7 +16,7 @@ use inkwell::{
|
|||
use itertools::{Either, Itertools};
|
||||
use tracing::info;
|
||||
|
||||
use crate::ast::{self, Expression, Function, LiteralValue, OpCode, Statement, TypeExp};
|
||||
use crate::ast::{self, Expression, Function, LiteralValue, OpCode, Spanned, Statement, TypeExp};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ProgramData {
|
||||
|
@ -95,22 +95,25 @@ impl<'ctx> CodeGen<'ctx> {
|
|||
|
||||
// create types
|
||||
for statement in &self.ast.statements {
|
||||
if let Statement::Struct(s) = &statement {
|
||||
if let Statement::Struct(s) = &statement.value {
|
||||
let mut fields = HashMap::new();
|
||||
let mut field_types = vec![];
|
||||
|
||||
for (i, field) in s.fields.iter().enumerate() {
|
||||
// todo: this doesnt handle out of order structs well
|
||||
let ty = self.get_llvm_type(&field.field_type)?;
|
||||
let ty = self.get_llvm_type(&field.field_type.value)?;
|
||||
field_types.push(ty);
|
||||
// todo: ensure alignment and padding here
|
||||
fields.insert(field.ident.clone(), (i, field.field_type.clone()));
|
||||
fields.insert(
|
||||
field.ident.value.clone(),
|
||||
(i, field.field_type.value.clone()),
|
||||
);
|
||||
}
|
||||
|
||||
let ty = self.context.struct_type(&field_types, false);
|
||||
|
||||
let struct_type = StructTypeInfo { fields, ty };
|
||||
struct_types.insert(s.name.clone(), struct_type);
|
||||
struct_types.insert(s.name.value.clone(), struct_type);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -118,8 +121,8 @@ impl<'ctx> CodeGen<'ctx> {
|
|||
|
||||
// create the llvm functions first.
|
||||
for statement in &self.ast.statements {
|
||||
if let Statement::Function(function) = &statement {
|
||||
functions.insert(function.name.clone(), function.clone());
|
||||
if let Statement::Function(function) = &statement.value {
|
||||
functions.insert(function.name.value.clone(), function.clone());
|
||||
self.compile_function_signature(function)?;
|
||||
}
|
||||
}
|
||||
|
@ -150,11 +153,11 @@ impl<'ctx> CodeGen<'ctx> {
|
|||
.as_basic_type_enum(),
|
||||
TypeExp::Boolean => self.context.bool_type().as_basic_type_enum(),
|
||||
TypeExp::Array { of, len } => {
|
||||
let ty = self.get_llvm_type(of)?;
|
||||
let ty = self.get_llvm_type(&of.value)?;
|
||||
ty.array_type(len.unwrap()).as_basic_type_enum()
|
||||
}
|
||||
TypeExp::Pointer { target } => {
|
||||
let ty = self.get_llvm_type(target)?;
|
||||
let ty = self.get_llvm_type(&target.value)?;
|
||||
ty.ptr_type(Default::default()).as_basic_type_enum()
|
||||
}
|
||||
TypeExp::Other { id } => self
|
||||
|
@ -172,7 +175,7 @@ impl<'ctx> CodeGen<'ctx> {
|
|||
.params
|
||||
.iter()
|
||||
.map(|param| ¶m.type_exp)
|
||||
.map(|t| self.get_llvm_type(t))
|
||||
.map(|t| self.get_llvm_type(&t.value))
|
||||
.try_collect()?;
|
||||
|
||||
let args_types: Vec<BasicMetadataTypeEnum<'ctx>> =
|
||||
|
@ -180,19 +183,20 @@ impl<'ctx> CodeGen<'ctx> {
|
|||
|
||||
let fn_type = match &function.return_type {
|
||||
Some(id) => {
|
||||
let return_type = self.get_llvm_type(id)?;
|
||||
let return_type = self.get_llvm_type(&id.value)?;
|
||||
return_type.fn_type(&args_types, false)
|
||||
}
|
||||
None => self.context.void_type().fn_type(&args_types, false),
|
||||
};
|
||||
|
||||
self.module.add_function(&function.name, fn_type, None);
|
||||
self.module
|
||||
.add_function(&function.name.value, fn_type, None);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn compile_function(&self, function: &Function) -> Result<()> {
|
||||
let func = self.module.get_function(&function.name).unwrap();
|
||||
let func = self.module.get_function(&function.name.value).unwrap();
|
||||
let entry_block = self.context.append_basic_block(func, "entry");
|
||||
|
||||
self.builder.position_at_end(entry_block);
|
||||
|
@ -205,7 +209,7 @@ impl<'ctx> CodeGen<'ctx> {
|
|||
.get_nth_param(i.try_into().unwrap())
|
||||
.expect("parameter");
|
||||
variables.insert(
|
||||
id.clone(),
|
||||
id.value.clone(),
|
||||
Variable {
|
||||
value: param_value,
|
||||
phi_counter: 0,
|
||||
|
@ -217,7 +221,7 @@ impl<'ctx> CodeGen<'ctx> {
|
|||
let mut has_return = false;
|
||||
|
||||
for statement in &function.body {
|
||||
if let Statement::Return(_) = statement {
|
||||
if let Statement::Return(_) = statement.value {
|
||||
has_return = true
|
||||
}
|
||||
self.compile_statement(func, statement, &mut variables, &function.scope_type_info)?;
|
||||
|
@ -233,12 +237,12 @@ impl<'ctx> CodeGen<'ctx> {
|
|||
fn compile_statement(
|
||||
&self,
|
||||
function_value: FunctionValue,
|
||||
statement: &Statement,
|
||||
statement: &Spanned<Statement>,
|
||||
// value, assignments
|
||||
variables: &mut Variables<'ctx>,
|
||||
scope_info: &HashMap<String, Vec<TypeExp>>,
|
||||
) -> Result<()> {
|
||||
match statement {
|
||||
match &statement.value {
|
||||
// Variable assignment
|
||||
Statement::Let {
|
||||
name,
|
||||
|
@ -247,11 +251,11 @@ impl<'ctx> CodeGen<'ctx> {
|
|||
..
|
||||
} => {
|
||||
let value = self
|
||||
.compile_expression(value, variables, scope_info)?
|
||||
.compile_expression(&value, variables, scope_info)?
|
||||
.expect("should have result");
|
||||
|
||||
variables.insert(
|
||||
name.clone(),
|
||||
name.value.clone(),
|
||||
Variable {
|
||||
value,
|
||||
phi_counter: 0,
|
||||
|
@ -261,17 +265,19 @@ impl<'ctx> CodeGen<'ctx> {
|
|||
}
|
||||
Statement::Mutate { name, value, .. } => {
|
||||
let value = self
|
||||
.compile_expression(value, variables, scope_info)?
|
||||
.compile_expression(&value, variables, scope_info)?
|
||||
.expect("should have result");
|
||||
|
||||
let var = variables.get_mut(name).expect("variable should exist");
|
||||
let var = variables
|
||||
.get_mut(&name.value)
|
||||
.expect("variable should exist");
|
||||
var.phi_counter += 1;
|
||||
var.value = value;
|
||||
}
|
||||
Statement::Return(ret) => {
|
||||
if let Some(ret) = ret {
|
||||
let value = self
|
||||
.compile_expression(ret, variables, scope_info)?
|
||||
.compile_expression(&ret, variables, scope_info)?
|
||||
.expect("should have result");
|
||||
self.builder.build_return(Some(&value));
|
||||
} else {
|
||||
|
@ -386,12 +392,12 @@ impl<'ctx> CodeGen<'ctx> {
|
|||
|
||||
pub fn compile_expression(
|
||||
&self,
|
||||
expr: &Expression,
|
||||
expr: &Spanned<Box<Expression>>,
|
||||
variables: &mut Variables<'ctx>,
|
||||
scope_info: &HashMap<String, Vec<TypeExp>>,
|
||||
) -> Result<Option<BasicValueEnum<'ctx>>> {
|
||||
Ok(match expr {
|
||||
Expression::Variable { name } => Some(self.compile_variable(&name.value, variables)?),
|
||||
Ok(match &*expr.value {
|
||||
Expression::Variable { name } => Some(self.compile_variable(&name, variables)?),
|
||||
Expression::Literal(term) => Some(self.compile_literal(term)?),
|
||||
Expression::Call { function, args } => {
|
||||
self.compile_call(function, args, variables, scope_info)?
|
||||
|
@ -404,13 +410,16 @@ impl<'ctx> CodeGen<'ctx> {
|
|||
|
||||
pub fn compile_call(
|
||||
&self,
|
||||
func_name: &str,
|
||||
args: &[Box<Expression>],
|
||||
func_name: &Spanned<String>,
|
||||
args: &[Spanned<Box<Expression>>],
|
||||
variables: &mut Variables<'ctx>,
|
||||
scope_info: &HashMap<String, Vec<TypeExp>>,
|
||||
) -> Result<Option<BasicValueEnum<'ctx>>> {
|
||||
info!("compiling fn call: func_name={}", func_name);
|
||||
let function = self.module.get_function(func_name).expect("should exist");
|
||||
info!("compiling fn call: func_name={}", func_name.value);
|
||||
let function = self
|
||||
.module
|
||||
.get_function(&func_name.value)
|
||||
.expect("should exist");
|
||||
|
||||
let mut value_args: Vec<BasicMetadataValueEnum> = Vec::with_capacity(args.len());
|
||||
|
||||
|
@ -423,7 +432,7 @@ impl<'ctx> CodeGen<'ctx> {
|
|||
|
||||
let result = self
|
||||
.builder
|
||||
.build_call(function, &value_args, &format!("{func_name}_call"))
|
||||
.build_call(function, &value_args, &format!("{}_call", func_name.value))
|
||||
.try_as_basic_value();
|
||||
|
||||
Ok(match result {
|
||||
|
@ -434,9 +443,9 @@ impl<'ctx> CodeGen<'ctx> {
|
|||
|
||||
pub fn compile_binary_op(
|
||||
&self,
|
||||
lhs: &Expression,
|
||||
lhs: &Spanned<Box<Expression>>,
|
||||
op: &OpCode,
|
||||
rhs: &Expression,
|
||||
rhs: &Spanned<Box<Expression>>,
|
||||
variables: &mut Variables<'ctx>,
|
||||
scope_info: &HashMap<String, Vec<TypeExp>>,
|
||||
) -> Result<BasicValueEnum<'ctx>> {
|
||||
|
@ -489,10 +498,9 @@ impl<'ctx> CodeGen<'ctx> {
|
|||
LiteralValue::Integer {
|
||||
value,
|
||||
bits,
|
||||
signed,
|
||||
signed: _,
|
||||
} => {
|
||||
let bits = *bits;
|
||||
let signed = *signed;
|
||||
|
||||
self.context
|
||||
.custom_width_int_type(bits)
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
use std::collections::HashMap;
|
||||
use crate::{
|
||||
ast::{self, Spanned},
|
||||
tokens::Token,
|
||||
|
@ -76,42 +75,46 @@ pub Program: ast::Program = {
|
|||
Statements => ast::Program::new(<>)
|
||||
}
|
||||
|
||||
Statements: Vec<ast::Statement> = {
|
||||
Statement => vec![<>],
|
||||
Statements: Vec<Spanned<ast::Statement>> = {
|
||||
<Statement> => vec![<>],
|
||||
<mut s:Statements> <n:Statement> => {
|
||||
s.push(n);
|
||||
s
|
||||
},
|
||||
};
|
||||
|
||||
Statement: ast::Statement = {
|
||||
Statement: Spanned<ast::Statement> = {
|
||||
BasicStatement,
|
||||
<f:Function> => ast::Statement::Function(f),
|
||||
<s:Struct> => ast::Statement::Struct(s),
|
||||
<lo:@L> <f:Function> <hi:@R> => Spanned::new(ast::Statement::Function(f), (lo, hi)),
|
||||
<lo:@L> <s:Struct> <hi:@R> => Spanned::new(ast::Statement::Struct(s), (lo, hi)),
|
||||
};
|
||||
|
||||
TypeInfo: ast::TypeExp = {
|
||||
TypeInfo: Spanned<ast::TypeExp> = {
|
||||
":" <i:LangType> => i
|
||||
};
|
||||
|
||||
Identifier: Spanned<String> = {
|
||||
<lo:@L> <i:"identifier"> <hi:@R> => Spanned::new(i, (lo, hi))
|
||||
}
|
||||
|
||||
// statements not including function definitions
|
||||
BasicStatement: ast::Statement = {
|
||||
<lo:@L> "let" <i:"identifier"> <t:TypeInfo?> "=" <e:Expr> ";" <hi:@R> =>
|
||||
ast::Statement::Let { name: i, value: e, value_type: t, span: (lo, hi) },
|
||||
<lo:@L> <i:"identifier"> "=" <e:Expr> ";" <hi:@R> =>
|
||||
ast::Statement::Mutate { name: i, value: e, span: (lo, hi) },
|
||||
"if" <cond:Expr> "{" <s:Statements> "}" <e:ElseExpr?> =>
|
||||
ast::Statement::If {
|
||||
BasicStatement: Spanned<ast::Statement> = {
|
||||
<lo:@L> "let" <i:Identifier> <t:TypeInfo?> "=" <e:Expr> ";" <hi:@R> =>
|
||||
Spanned::new(ast::Statement::Let { name: i, value: e, value_type: t }, (lo, hi)),
|
||||
<lo:@L> <i:Identifier> "=" <e:Expr> ";" <hi:@R> =>
|
||||
Spanned::new(ast::Statement::Mutate { name: i, value: e }, (lo, hi)),
|
||||
<lo:@L> "if" <cond:Expr> "{" <s:Statements> "}" <e:ElseExpr?> <hi:@R> =>
|
||||
Spanned::new(ast::Statement::If {
|
||||
condition: cond,
|
||||
body: s,
|
||||
else_body: e,
|
||||
scope_type_info: Default::default(),
|
||||
else_body_scope_type_info: Default::default(),
|
||||
},
|
||||
"return" <e:Expr?> ";" => ast::Statement::Return(e),
|
||||
}, (lo, hi)),
|
||||
<lo:@L> "return" <e:Expr?> ";" <hi:@R> => Spanned::new(ast::Statement::Return(e), (lo, hi)),
|
||||
};
|
||||
|
||||
ElseExpr: Vec<ast::Statement> = {
|
||||
ElseExpr: Vec<Spanned<ast::Statement>> = {
|
||||
"else" "{" <s:Statements> "}" => s
|
||||
}
|
||||
|
||||
|
@ -136,8 +139,8 @@ Level3_Op: ast::OpCode = {
|
|||
"%" => ast::OpCode::Rem,
|
||||
}
|
||||
|
||||
Tier<Op,NextTier>: Box<ast::Expression> = {
|
||||
<t:Tier<Op,NextTier>> <o:Op> <n:NextTier> => Box::new(ast::Expression::BinaryOp(t, o, n)),
|
||||
Tier<Op,NextTier>: Spanned<Box<ast::Expression>> = {
|
||||
<lo:@L> <t:Tier<Op,NextTier>> <o:Op> <n:NextTier> <hi:@R> => Spanned::new(Box::new(ast::Expression::BinaryOp(t, o, n)), (lo, hi)),
|
||||
NextTier
|
||||
};
|
||||
|
||||
|
@ -147,14 +150,14 @@ Expr3 = Tier<Level2_Op, Expr4>;
|
|||
Expr4 = Tier<Level3_Op, Term>;
|
||||
|
||||
// Terms: variables, literals, calls
|
||||
Term: Box<ast::Expression> = {
|
||||
<lo:@L> <i:"identifier"> <hi:@R> => Box::new(ast::Expression::Variable {
|
||||
name: Spanned::new(i, (lo, hi))
|
||||
}),
|
||||
<n:Number> => Box::new(ast::Expression::Literal(n)),
|
||||
<n:StringLit> => Box::new(ast::Expression::Literal(n)),
|
||||
<n:BoolLiteral> => Box::new(ast::Expression::Literal(n)),
|
||||
<i:"identifier"> "(" <values:Comma<Term>> ")" => Box::new(ast::Expression::Call { function: i, args: values }),
|
||||
Term: Spanned<Box<ast::Expression>> = {
|
||||
<lo:@L> <i:"identifier"> <hi:@R> => Spanned::new(Box::new(ast::Expression::Variable {
|
||||
name: i
|
||||
}), (lo, hi)),
|
||||
<lo:@L> <n:Number> <hi:@R> => Spanned::new(Box::new(ast::Expression::Literal(n)), (lo, hi)),
|
||||
<lo:@L> <n:StringLit> <hi:@R> => Spanned::new(Box::new(ast::Expression::Literal(n)), (lo, hi)),
|
||||
<lo:@L> <n:BoolLiteral> <hi:@R> => Spanned::new(Box::new(ast::Expression::Literal(n)), (lo, hi)),
|
||||
<lo:@L> <i:Identifier> "(" <values:Comma<Term>> ")" <hi:@R> => Spanned::new(Box::new(ast::Expression::Call { function: i, args: values }), (lo, hi)),
|
||||
"(" <Term> ")"
|
||||
};
|
||||
|
||||
|
@ -214,46 +217,46 @@ ArrayLen: u32 = {
|
|||
";" <i:"int literal"> => i.parse().unwrap(),
|
||||
}
|
||||
|
||||
LangType: ast::TypeExp = {
|
||||
"ptr" "<" <target:LangType> ">" => ast::TypeExp::Pointer { target: Box::new(target) },
|
||||
"[" <of:LangType> <len:ArrayLen?> "]" => ast::TypeExp::Array { of: Box::new(of), len },
|
||||
"i8" => ast::TypeExp::Integer { bits: 8, signed: true },
|
||||
"i16" => ast::TypeExp::Integer { bits: 16, signed: true },
|
||||
"i32" => ast::TypeExp::Integer { bits: 32, signed: true },
|
||||
"i64" => ast::TypeExp::Integer { bits: 64, signed: true },
|
||||
"u8" => ast::TypeExp::Integer { bits: 8, signed: false },
|
||||
"u16" => ast::TypeExp::Integer { bits: 16, signed: false },
|
||||
"u32" => ast::TypeExp::Integer { bits: 32, signed: false },
|
||||
"u64" => ast::TypeExp::Integer { bits: 64, signed: false },
|
||||
"bool" => ast::TypeExp::Boolean,
|
||||
<id:"identifier"> => ast::TypeExp::Other { id },
|
||||
LangType: Spanned<ast::TypeExp> = {
|
||||
<lo:@L> "ptr" "<" <target:LangType> ">" <hi:@R> => Spanned::new(ast::TypeExp::Pointer { target: Spanned::new(Box::new(target.value), target.span) }, (lo, hi)),
|
||||
<lo:@L> "[" <of:LangType> <len:ArrayLen?> "]" <hi:@R> => Spanned::new(ast::TypeExp::Array { of: Spanned::new(Box::new(of.value), of.span), len }, (lo, hi)),
|
||||
<lo:@L> "i8" <hi:@R> => Spanned::new(ast::TypeExp::Integer { bits: 8, signed: true }, (lo, hi)),
|
||||
<lo:@L> "i16" <hi:@R> => Spanned::new(ast::TypeExp::Integer { bits: 16, signed: true }, (lo, hi)),
|
||||
<lo:@L> "i32" <hi:@R> => Spanned::new(ast::TypeExp::Integer { bits: 32, signed: true }, (lo, hi)),
|
||||
<lo:@L> "i64" <hi:@R> => Spanned::new(ast::TypeExp::Integer { bits: 64, signed: true }, (lo, hi)),
|
||||
<lo:@L> "u8" <hi:@R> => Spanned::new(ast::TypeExp::Integer { bits: 8, signed: false }, (lo, hi)),
|
||||
<lo:@L> "u16" <hi:@R> => Spanned::new(ast::TypeExp::Integer { bits: 16, signed: false }, (lo, hi)),
|
||||
<lo:@L> "u32" <hi:@R> => Spanned::new(ast::TypeExp::Integer { bits: 32, signed: false }, (lo, hi)),
|
||||
<lo:@L> "u64" <hi:@R> => Spanned::new(ast::TypeExp::Integer { bits: 64, signed: false }, (lo, hi)),
|
||||
<lo:@L> "bool" <hi:@R> => Spanned::new(ast::TypeExp::Boolean, (lo, hi)),
|
||||
<lo:@L> <id:"identifier"> <hi:@R> => Spanned::new(ast::TypeExp::Other { id }, (lo, hi)),
|
||||
};
|
||||
|
||||
// Function handling
|
||||
Param: ast::Parameter = {
|
||||
<"identifier"> ":" <LangType> => ast::Parameter::new(<>)
|
||||
<Identifier> ":" <LangType> => ast::Parameter::new(<>)
|
||||
};
|
||||
|
||||
Params = Comma<Param>;
|
||||
|
||||
FunctionReturn: ast::TypeExp = {
|
||||
FunctionReturn: Spanned<ast::TypeExp> = {
|
||||
"->" <i:LangType> => i,
|
||||
}
|
||||
|
||||
Function: ast::Function = {
|
||||
"fn" <i:"identifier"> "(" <a:Params> ")" <r:FunctionReturn?> "{" <s:Statements> "}" => ast::Function::new(i, a, s, r)
|
||||
"fn" <i:Identifier> "(" <a:Params> ")" <r:FunctionReturn?> "{" <s:Statements> "}" => ast::Function::new(i, a, s, r)
|
||||
}
|
||||
|
||||
// Structures
|
||||
|
||||
StructField: ast::StructField = {
|
||||
<"identifier"> ":" <LangType> => ast::StructField::new(<>)
|
||||
<Identifier> ":" <LangType> => ast::StructField::new(<>)
|
||||
};
|
||||
|
||||
StructFields = Comma<StructField>;
|
||||
|
||||
Struct: ast::Struct = {
|
||||
"struct" <i:"identifier"> "{" <fields:StructFields> "}" => {
|
||||
"struct" <i:Identifier> "{" <fields:StructFields> "}" => {
|
||||
ast::Struct {
|
||||
name: i,
|
||||
fields
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
use crate::ast::{self, Expression, Function, Statement, TypeExp};
|
||||
use crate::ast::{self, Expression, Function, Spanned, Statement, TypeExp};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum TypeError {
|
||||
|
@ -30,19 +30,19 @@ pub fn type_check(ast: &mut ast::Program) -> Result<(), TypeError> {
|
|||
|
||||
// gather global constructs first
|
||||
for statement in ast.statements.iter_mut() {
|
||||
match statement {
|
||||
match &mut statement.value {
|
||||
Statement::Struct(st) => {
|
||||
let fields = st
|
||||
.fields
|
||||
.iter()
|
||||
.map(|x| (x.ident.clone(), x.field_type.clone()))
|
||||
.map(|x| (x.ident.value.clone(), x.field_type.value.clone()))
|
||||
.collect();
|
||||
storage.structs.insert(st.name.clone(), fields);
|
||||
storage.structs.insert(st.name.value.clone(), fields);
|
||||
}
|
||||
Statement::Function(function) => {
|
||||
storage
|
||||
.functions
|
||||
.insert(function.name.clone(), function.clone());
|
||||
.insert(function.name.value.clone(), function.clone());
|
||||
}
|
||||
// todo: find globals here too
|
||||
_ => {}
|
||||
|
@ -50,11 +50,14 @@ pub fn type_check(ast: &mut ast::Program) -> Result<(), TypeError> {
|
|||
}
|
||||
|
||||
for statement in ast.statements.iter_mut() {
|
||||
if let Statement::Function(function) = statement {
|
||||
if let Statement::Function(function) = &mut statement.value {
|
||||
let mut scope_vars: ScopeMap = HashMap::new();
|
||||
|
||||
for arg in &function.params {
|
||||
scope_vars.insert(arg.ident.clone(), vec![Some(arg.type_exp.clone())]);
|
||||
scope_vars.insert(
|
||||
arg.ident.value.clone(),
|
||||
vec![Some(arg.type_exp.value.clone())],
|
||||
);
|
||||
}
|
||||
|
||||
let func_info = function.clone();
|
||||
|
@ -72,7 +75,7 @@ pub fn type_check(ast: &mut ast::Program) -> Result<(), TypeError> {
|
|||
|
||||
/// Finds variable types in the scope, returns newly created variables to handle shadowing
|
||||
fn type_inference_scope(
|
||||
statements: &mut [ast::Statement],
|
||||
statements: &mut [Spanned<ast::Statement>],
|
||||
scope_vars: &ScopeMap,
|
||||
func: &Function,
|
||||
storage: &Storage,
|
||||
|
@ -81,43 +84,45 @@ fn type_inference_scope(
|
|||
let mut new_vars: HashSet<String> = HashSet::new();
|
||||
|
||||
for statement in statements {
|
||||
match statement {
|
||||
match &mut statement.value {
|
||||
Statement::Let {
|
||||
name,
|
||||
value,
|
||||
value_type,
|
||||
span,
|
||||
} => {
|
||||
new_vars.insert(name.clone());
|
||||
new_vars.insert(name.value.clone());
|
||||
|
||||
let exp_type = type_inference_expression(value, &mut scope_vars, storage, None)?;
|
||||
let exp_type = type_inference_expression(&value, &mut scope_vars, storage, None)?;
|
||||
|
||||
if !scope_vars.contains_key(name) {
|
||||
scope_vars.insert(name.clone(), vec![]);
|
||||
if !scope_vars.contains_key(&name.value) {
|
||||
scope_vars.insert(name.value.clone(), vec![]);
|
||||
}
|
||||
|
||||
let var = scope_vars.get_mut(name).unwrap();
|
||||
let var = scope_vars.get_mut(&name.value).unwrap();
|
||||
|
||||
if value_type.is_none() {
|
||||
var.push(exp_type);
|
||||
} else {
|
||||
if exp_type.is_some() && &exp_type != value_type {
|
||||
if exp_type.is_some() && exp_type != value_type.clone().map(|x| x.value) {
|
||||
Err(TypeError::Mismatch {
|
||||
found: exp_type.clone().unwrap(),
|
||||
expected: value_type.clone().unwrap(),
|
||||
span: *span,
|
||||
expected: value_type.clone().map(|x| x.value).unwrap(),
|
||||
span: statement.span,
|
||||
})?;
|
||||
}
|
||||
var.push(value_type.clone());
|
||||
var.push(value_type.clone().map(|x| x.value));
|
||||
}
|
||||
}
|
||||
Statement::Mutate { name, value, span } => {
|
||||
if !scope_vars.contains_key(name) {
|
||||
panic!("undeclared variable");
|
||||
Statement::Mutate { name, value } => {
|
||||
if !scope_vars.contains_key(&name.value) {
|
||||
Err(TypeError::UndeclaredVariable {
|
||||
name: name.value.clone(),
|
||||
span: name.span,
|
||||
})?;
|
||||
}
|
||||
|
||||
let exp_type = type_inference_expression(value, &mut scope_vars, storage, None)?;
|
||||
let var = scope_vars.get_mut(name).unwrap().last_mut().unwrap();
|
||||
let exp_type = type_inference_expression(&value, &mut scope_vars, storage, None)?;
|
||||
let var = scope_vars.get_mut(&name.value).unwrap().last_mut().unwrap();
|
||||
|
||||
if var.is_none() {
|
||||
*var = exp_type;
|
||||
|
@ -125,7 +130,7 @@ fn type_inference_scope(
|
|||
Err(TypeError::Mismatch {
|
||||
found: exp_type.clone().unwrap(),
|
||||
expected: var.clone().unwrap(),
|
||||
span: *span,
|
||||
span: statement.span,
|
||||
})?;
|
||||
}
|
||||
}
|
||||
|
@ -137,7 +142,7 @@ fn type_inference_scope(
|
|||
else_body_scope_type_info,
|
||||
} => {
|
||||
type_inference_expression(
|
||||
condition,
|
||||
&condition,
|
||||
&mut scope_vars,
|
||||
storage,
|
||||
Some(TypeExp::Boolean),
|
||||
|
@ -181,7 +186,7 @@ fn type_inference_scope(
|
|||
exp,
|
||||
&mut scope_vars,
|
||||
storage,
|
||||
func.return_type.clone(),
|
||||
func.return_type.clone().map(|x| x.value),
|
||||
)?;
|
||||
}
|
||||
}
|
||||
|
@ -194,12 +199,12 @@ fn type_inference_scope(
|
|||
}
|
||||
|
||||
fn type_inference_expression(
|
||||
exp: &Expression,
|
||||
exp: &Spanned<Box<Expression>>,
|
||||
scope_vars: &mut ScopeMap,
|
||||
storage: &Storage,
|
||||
expected_type: Option<TypeExp>,
|
||||
) -> Result<Option<TypeExp>, TypeError> {
|
||||
Ok(match exp {
|
||||
Ok(match &*exp.value {
|
||||
Expression::Literal(lit) => {
|
||||
match lit {
|
||||
ast::LiteralValue::String(_) => None, // todo
|
||||
|
@ -216,7 +221,7 @@ fn type_inference_expression(
|
|||
}
|
||||
Expression::Variable { name } => {
|
||||
let var = scope_vars
|
||||
.get_mut(&name.value)
|
||||
.get_mut(name)
|
||||
.expect("to exist")
|
||||
.last_mut()
|
||||
.unwrap();
|
||||
|
@ -230,7 +235,7 @@ fn type_inference_expression(
|
|||
Err(TypeError::Mismatch {
|
||||
found: expected_type.clone().unwrap(),
|
||||
expected: var.clone().unwrap(),
|
||||
span: name.span,
|
||||
span: exp.span,
|
||||
})?;
|
||||
}
|
||||
expected_type
|
||||
|
@ -242,15 +247,15 @@ fn type_inference_expression(
|
|||
}
|
||||
}
|
||||
Expression::Call { function, args } => {
|
||||
let func = storage.functions.get(function).cloned().unwrap();
|
||||
let func = storage.functions.get(&function.value).cloned().unwrap();
|
||||
|
||||
for (i, arg) in args.iter().enumerate() {
|
||||
let arg_type = func.params[i].type_exp.clone();
|
||||
// result is ignored, but need these to infer call arg types
|
||||
type_inference_expression(arg, scope_vars, storage, Some(arg_type))?;
|
||||
type_inference_expression(arg, scope_vars, storage, Some(arg_type.value))?;
|
||||
}
|
||||
|
||||
func.return_type
|
||||
func.return_type.map(|x| x.value)
|
||||
}
|
||||
Expression::BinaryOp(lhs, op, rhs) => match op {
|
||||
ast::OpCode::Eq | ast::OpCode::Ne => Some(TypeExp::Boolean),
|
||||
|
|
Loading…
Reference in a new issue