working on adding typing

This commit is contained in:
Andrew Segavac
2020-07-23 23:54:01 -06:00
parent ba65669225
commit 9db464a726
8 changed files with 391 additions and 158 deletions

View File

@@ -1,5 +1,23 @@
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub struct Span {
pub left: usize,
pub right: usize
}
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub struct Spanned<T> {
pub span: Span,
pub value: T,
}
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub struct TypeUsage {
pub name: Spanned<Identifier>
//TODO: Generics go here
}
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub enum Operator {
Mul,
Div,
@@ -7,42 +25,72 @@ pub enum Operator {
Minus,
}
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub struct LiteralInt {
pub value: i64
pub value: i64,
}
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub struct Identifier {
pub name: String
pub name: String,
}
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub struct FunctionCall {
pub name: Identifier,
pub arguments: Vec<Box<Expression>>,
pub name: Spanned<Identifier>,
pub arguments: Vec<Spanned<Box<Expression>>>,
}
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub struct Operation {
pub left: Spanned<Box<Expression>>,
pub op: Operator,
pub right: Spanned<Box<Expression>>,
}
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub enum Expression {
LiteralInt(LiteralInt),
FunctionCall(FunctionCall),
Identifier(Identifier),
Op(Box<Expression>, Operator, Box<Expression>),
LiteralInt(Spanned<LiteralInt>),
FunctionCall(Spanned<FunctionCall>),
Identifier(Spanned<Identifier>),
Op(Operation),
}
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub struct Assignment {
pub name: Spanned<Identifier>,
pub type_usage: Option<Spanned<TypeUsage>>,
// mut, weak here
pub expression: Spanned<Box<Expression>>,
}
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub enum Statement {
Assignment(Spanned<Assignment>),
Expression(Spanned<Box<Expression>>),
}
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub struct Block {
pub expression: Box<Expression>
pub statements: Vec<Spanned<Statement>>,
}
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub struct VariableDeclaration {
pub name: Identifier,
pub name: Spanned<Identifier>,
pub type_usage: Spanned<TypeUsage>,
}
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub struct Function {
pub name: Identifier,
pub name: Spanned<Identifier>,
pub arguments: Vec<VariableDeclaration>,
pub return_type: Spanned<TypeUsage>,
pub block: Block,
}
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub struct Module {
pub functions: Vec<Function>,
}

View File

@@ -32,10 +32,10 @@ impl<'ctx> ModuleCodeGen<'ctx> {
self.context.i64_type().const_int(unsafe { mem::transmute::<i64, u64>(literal_int.value) }, true)
}
pub fn gen_op_expression(&mut self, scope: &Scope<'ctx>, lhs: &Box<ast::Expression>, op: &ast::Operator, rhs: &Box<ast::Expression>) -> IntValue<'ctx> {
let lhs_result = self.gen_expression(scope, lhs);
let rhs_result = self.gen_expression(scope, rhs);
self.gen_op_int(&lhs_result, &rhs_result, op)
pub fn gen_op_expression(&mut self, scope: &Scope<'ctx>, operation: &ast::Operation) -> IntValue<'ctx> {
let lhs_result = self.gen_expression(scope, &operation.left);
let rhs_result = self.gen_expression(scope, &operation.right);
self.gen_op_int(&lhs_result, &rhs_result, &operation.op)
}
pub fn gen_op_int(&mut self, lhs: &IntValue<'ctx>, rhs: &IntValue<'ctx>, op: &ast::Operator) -> IntValue<'ctx> {
@@ -57,7 +57,7 @@ impl<'ctx> ModuleCodeGen<'ctx> {
}
},
ast::Expression::FunctionCall(function_call) => self.gen_function_call(scope, &function_call),
ast::Expression::Op(lhs, op, rhs) => self.gen_op_expression(scope, &lhs, &op, &rhs),
ast::Expression::Op(operation) => self.gen_op_expression(scope, &operation),
}
}

View File

@@ -6,46 +6,62 @@ grammar;
pub LiteralInt: ast::LiteralInt = {
r"[0-9]+" => ast::LiteralInt{value: i64::from_str(<>).unwrap()}
<literal:r"[0-9]+"> => ast::LiteralInt{value: i64::from_str(literal).unwrap()}
};
pub Identifier: ast::Identifier = {
r"[A-Za-z][A-Za-z0-9_]*" => ast::Identifier{name: <>.to_string()}
<i:r"[A-Za-z][A-Za-z0-9_]*"> => ast::Identifier{name: i.to_string()}
};
pub FunctionCall: ast::FunctionCall = {
<i:Identifier> "(" <args:Comma<Expression>> ")" => ast::FunctionCall{name:i, arguments: args}
<i:Spanned<Identifier>> "(" <args:Comma<Spanned<Expression>>> ")" => ast::FunctionCall{name:i, arguments: args}
}
pub Expression: Box<ast::Expression> = {
<l:Expression> "+" <r:Factor> => Box::new(ast::Expression::Op(l, ast::Operator::Plus, r)),
<l:Expression> "-" <r:Factor> => Box::new(ast::Expression::Op(l, ast::Operator::Minus, r)),
<l:Spanned<Expression>> "+" <r:Spanned<Factor>> => Box::new(ast::Expression::Op(ast::Operation{left: l, op: ast::Operator::Plus, right: r})),
<l:Spanned<Expression>> "-" <r:Spanned<Factor>> => Box::new(ast::Expression::Op(ast::Operation{left: l, op: ast::Operator::Minus, right: r})),
Factor,
}
pub Factor: Box<ast::Expression> = {
<l:Factor> "*" <r:Term> => Box::new(ast::Expression::Op(l, ast::Operator::Mul, r)),
<l:Factor> "/" <r:Term> => Box::new(ast::Expression::Op(l, ast::Operator::Div, r)),
<l:Spanned<Factor>> "*" <r:Spanned<Term>> => Box::new(ast::Expression::Op(ast::Operation{left: l, op: ast::Operator::Mul, right: r})),
<l:Spanned<Factor>> "/" <r:Spanned<Term>> => Box::new(ast::Expression::Op(ast::Operation{left: l, op: ast::Operator::Div, right: r})),
Term,
}
pub Term: Box<ast::Expression> = {
LiteralInt => Box::new(ast::Expression::LiteralInt(<>)),
Identifier => Box::new(ast::Expression::Identifier(<>)),
<FunctionCall> => Box::new(ast::Expression::FunctionCall(<>)),
"(" <Expression> ")",
Spanned<LiteralInt> => Box::new(ast::Expression::LiteralInt(<>)),
Spanned<Identifier> => Box::new(ast::Expression::Identifier(<>)),
Spanned<FunctionCall> => Box::new(ast::Expression::FunctionCall(<>)),
"(" <e:Expression> ")" => e
}
pub LetStatement: ast::Assignment = {
//TODO: support destructuring with tuples, when they exist.
//TODO: add mut, weak
"let" <n:Spanned<Identifier>> "=" <e:Spanned<Expression>> => ast::Assignment{name: n, type_usage: None, expression: e},
"let" <n:Spanned<Identifier>> ":" <t:Spanned<TypeUsage>> "=" <e:Spanned<Expression>> => ast::Assignment{name: n, type_usage: Some(t), expression: e},
}
pub Statement: ast::Statement = {
<l:Spanned<LetStatement>> => ast::Statement::Assignment(l),
<e:Spanned<Expression>> => ast::Statement::Expression(e),
}
pub Block: ast::Block = {
"{" <e:Expression> "}" => ast::Block{expression: e}
"{" <s:LineDelimiter<Spanned<Statement>>> "}" => ast::Block{statements: s}
}
pub TypeUsage: ast::TypeUsage = {
<n:Spanned<Identifier>> => ast::TypeUsage{name: n}
}
pub VariableDeclaration: ast::VariableDeclaration = {
Identifier => ast::VariableDeclaration{name: <>}
<i:Spanned<Identifier>> ":" <t:Spanned<TypeUsage>> => ast::VariableDeclaration{name: i, type_usage: t},
}
pub Function: ast::Function = {
"fn" <n:Identifier> "(" <args:Comma<VariableDeclaration>> ")" <b:Block> => ast::Function{name: n, arguments: args, block: b}
"fn" <n:Spanned<Identifier>> "(" <args:Comma<VariableDeclaration>> ")" <rt:Spanned<TypeUsage>> <b:Block> => ast::Function{name: n, arguments: args, block: b, return_type: rt}
}
@@ -66,3 +82,17 @@ Comma<T>: Vec<T> = {
}
}
};
LineDelimiter<T>: Vec<T> = {
<v:(<T> r"\n|;")*> <e:T> => {
let mut v = v;
v.push(e);
v
}
};
Spanned<Rule>: ast::Spanned<Rule> = {
<l: @L> <rule: Rule> <r: @R> => ast::Spanned{span: ast::Span{left: l, right: r}, value: rule}
};

0
src/hir/body.rs Normal file
View File

31
src/hir/errors.rs Normal file
View File

@@ -0,0 +1,31 @@
use std::error::Error;
use std::fmt;
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum ErrorLabel {
Primary,
Secondary,
}
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub struct ErrorPart {
pub message: String,
pub label_type: ErrorLabel,
pub start: i64
pub end: i64
}
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub struct CompilerError {
pub message: String,
pub parts: Vec<ErrorPart>,
}
impl fmt::Display for CompilerError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, self.message)
}
}

156
src/hir/types.rs Normal file
View File

@@ -0,0 +1,156 @@
use inkwell::values::{BasicValueEnum, FunctionValue, IntValue};
use inkwell::context::Context;
use crate::ast;
use crate::hir:errors;
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum Signedness {
Signed,
Unsigned,
}
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum IntBitness {
X8,
X16,
X32,
X64,
X128,
}
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum FloatBitness {
X32,
X64,
X128,
}
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
pub struct IntTypeDef {
pub signedness: Signedness,
pub bitness: IntBitness,
}
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub struct FloatTypeDef {
pub bitness: FloatBitness,
}
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub struct FunctionTypeDef {
pub arguments: Vec<Type>,
pub return_type: Type,
}
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub enum Type {
Bool,
Int(IntTypeDef),
Float(FloatTypeDef),
Function(FunctionTypeDef),
// String(StringTypeDef),
// Struct(StructTypeDef),
// Trait(TraitTypeDef),
// Void,
// Never,
}
impl Type {
/// Construct an inkwell value for this type. Will eventually take in a list of types for
/// generic params.
pub fn construct(&self, context: Context) -> BasicValueEnum {
match self {
Bool => context.bool_type(),
Int(type_def) => {
// Signed + Unsigned use the same size, builder operations determine signedness
match type_def.bitness {
X8 => context.i8_type(),
X16 => context.i16_type(),
X32 => context.i32_type(),
X64 => context.i64_type(),
X128 => context.i128_type(),
}
},
Float(type_def) => {
match type_def.bitness {
X32 => context.f32_type(),
X64 => context.f64_type(),
X128 => context.f128_type(),
}
},
Function(type_def) => {
let return_type = type_def.return_type.construct(context);
let mut args = Vec::new();
for arg in &type_def.arguments {
args.push(arg.construct(context));
}
return_type.fn_type(&args, false)
},
}
}
}
/// Used for places where type info may or may not be solved.
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
pub enum SpecifiedType {
Unknown,
Type(Type),
}
/// Used by HIR in any place that needs a type. Allows `Unknown` types for places that haven't
/// been solved yet, and will verify/solve types via the `Compare` method. This will also
/// eventually contain info from escape analysis for if it will need to be created on the stack.
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
pub struct TypeLocation {
pub specified_type: SpecifiedType,
// pub type_parameters: Vec<TypeLocation>, // will be used for generics
pub span: ast::Span,
}
impl TypeLocation {
// Checks if the types are valid to be used this way, returns true
// if we updated an unknown.
pub fn compare(&mut self, other: &mut Self) -> Result<bool, errors::CompilerError> {
match self.specified_type {
SpecifiedType::Unknown => match other.specified_type {
SpecifiedType::Unknown => false,
SpecifiedType::Type(ty) => {
self.specified_type = ty;
true
},
},
SpecifiedType::Type(ty) => match other.specified_type {
SpecifiedType::Unknown => {
other.specified_type = ty;
true
},
SpecifiedType::Type(other_ty) => {
if ty == other_ty {
false
} else {
Err(errors::CompilerError{
message: "types do not match",
parts: vec![errors::ErrorPart{
message: format!("type {} must match", self.specified_type),
label_type: errors::ErrorLabel::Primary,
start: self.span.left,
end: self.span.right,
}, errors::ErrorPart{
message: format!("type {}", other.specified_type),
label_type: errors::ErrorLabel::Primary,
start: other.span.left,
end: other.span.right,
}],
})
}
}
}
}
}
}

View File

@@ -5,7 +5,7 @@ lalrpop_mod!(pub grammar); // synthesized by LALRPOP
use std::fs;
use std::io::Write;
mod compiler;
// mod compiler;
use inkwell::context::Context;
extern crate clap;
use clap::{Arg, App};
@@ -40,12 +40,12 @@ fn main() {
let module_ast = grammar::ModuleParser::new().parse(&contents).unwrap(); //TODO: convert to error
let context = Context::create();
let mut code_gen = compiler::ModuleCodeGen::new(&context, "main".to_string());
code_gen.gen_module(module_ast);
let mut f = fs::File::create(output).expect("Unable to create out file");
f.write_all(code_gen.dump().as_bytes()).expect("Unable to write data");
// let context = Context::create();
// let mut code_gen = compiler::ModuleCodeGen::new(&context, "main".to_string());
// code_gen.gen_module(module_ast);
//
// let mut f = fs::File::create(output).expect("Unable to create out file");
// f.write_all(code_gen.dump().as_bytes()).expect("Unable to write data");
}
@@ -63,18 +63,21 @@ fn grammar() {
assert!(grammar::ExpressionParser::new().parse("(22 * 33) + 24").is_ok());
assert!(grammar::BlockParser::new().parse("{ (22 * 33) + 24 }").is_ok());
assert!(grammar::BlockParser::new().parse("{ (22 * 33) + 24; 24 }").is_ok());
// assert!(grammar::BlockParser::new().parse("{ (22 * 33) + 24\n 24 }").is_ok());
assert!(grammar::BlockParser::new().parse("{ }").is_err());
assert!(grammar::VariableDeclarationParser::new().parse("foo").is_ok());
assert!(grammar::VariableDeclarationParser::new().parse("foo: Int32").is_ok());
assert!(grammar::VariableDeclarationParser::new().parse("foo").is_err());
assert!(grammar::VariableDeclarationParser::new().parse("1234").is_err());
assert!(grammar::FunctionParser::new().parse("fn add(a, b) { a + b }").is_ok());
assert!(grammar::FunctionParser::new().parse("fn random_dice_roll() { 4 }").is_ok());
assert!(grammar::FunctionParser::new().parse("fn add(a, b) { a + }").is_err());
assert!(grammar::FunctionParser::new().parse("fn add(a, b)").is_err());
assert!(grammar::FunctionParser::new().parse("fn add(a: Int32, b: Int32) Int32 { a + b }").is_ok());
assert!(grammar::FunctionParser::new().parse("fn random_dice_roll() Int32 { 4 }").is_ok());
assert!(grammar::FunctionParser::new().parse("fn add(a: Int32, b: Int32) Int32 { a + }").is_err());
assert!(grammar::FunctionParser::new().parse("fn add(a: Int32, b: Int32) Int32").is_err());
assert!(grammar::FunctionCallParser::new().parse("foo(1, 2)").is_ok());
assert!(grammar::ModuleParser::new().parse("fn add(a, b) { a + b }").is_ok());
assert!(grammar::ModuleParser::new().parse("fn add(a, b) { a + b } fn subtract(a, b) { a - b }").is_ok());
assert!(grammar::ModuleParser::new().parse("fn add(a: Int32, b: Int32) Int32 { a + b }").is_ok());
assert!(grammar::ModuleParser::new().parse("fn add(a: Int32, b: Int32) Int32 { a + b } fn subtract(a: Int32, b: Int32) Int32 { a - b }").is_ok());
}