Compare commits
3 Commits
8a32e66ba8
...
0392c293ba
Author | SHA1 | Date | |
---|---|---|---|
0392c293ba | |||
61ba00ea58 | |||
a66fc61c07 |
@ -6,7 +6,7 @@
|
||||
# Do note this file is extremely simply for my own personal convenience
|
||||
|
||||
export .env
|
||||
cargo run --example testcodegen && \
|
||||
cargo run --example $1 && \
|
||||
# clang hello.o -o main && \
|
||||
ld -dynamic-linker /lib64/ld-linux-x86-64.so.2 \
|
||||
-o main /usr/lib/crt1.o hello.o -lc && \
|
||||
|
11
reid/examples/fibonacci.rs
Normal file
11
reid/examples/fibonacci.rs
Normal file
@ -0,0 +1,11 @@
|
||||
use reid::compile;
|
||||
|
||||
pub static FIBONACCI: &str = include_str!("./reid/fibonacci.reid");
|
||||
|
||||
fn main() {
|
||||
let text = match compile(FIBONACCI) {
|
||||
Ok(t) => t,
|
||||
Err(e) => panic!("{}", e),
|
||||
};
|
||||
println!("{}", text);
|
||||
}
|
@ -3,12 +3,11 @@
|
||||
fn main() {
|
||||
let test = 9;
|
||||
let simpleAdd = 2 + 2;
|
||||
let simpleMult = 7 * 2; // 14
|
||||
let arithmetic = 3 + 2 * 5 + 1 * 2; // 15
|
||||
let simpleSub = 7 - 2; // 14
|
||||
|
||||
if simpleAdd < test {
|
||||
return 3;
|
||||
}
|
||||
|
||||
return arithmetic + simpleMult * arithmetic;
|
||||
return arithmetic + simpleSub + boop;
|
||||
}
|
@ -11,115 +11,114 @@ fn main() {
|
||||
kind: FunctionDefinitionKind::Local(
|
||||
Block {
|
||||
statements: vec![Statement(
|
||||
StatementKind::If(IfExpression(
|
||||
// If N < 3
|
||||
Box::new(Expression(
|
||||
ExpressionKind::BinOp(
|
||||
BinaryOperator::Logic(LogicOperator::GreaterThan),
|
||||
Box::new(Expression(
|
||||
ExpressionKind::Variable(VariableReference(
|
||||
TypeKind::I32,
|
||||
"N".to_string(),
|
||||
StmtKind::Expression(Expression(
|
||||
ExprKind::If(IfExpression(
|
||||
// If N < 3
|
||||
Box::new(Expression(
|
||||
ExprKind::BinOp(
|
||||
BinaryOperator::Logic(LogicOperator::GreaterThan),
|
||||
Box::new(Expression(
|
||||
ExprKind::Variable(VariableReference(
|
||||
TypeKind::I32,
|
||||
"N".to_string(),
|
||||
Default::default(),
|
||||
)),
|
||||
Default::default(),
|
||||
)),
|
||||
Default::default(),
|
||||
)),
|
||||
Box::new(Expression(
|
||||
ExpressionKind::Literal(Literal::I32(2)),
|
||||
Default::default(),
|
||||
)),
|
||||
),
|
||||
Default::default(),
|
||||
)),
|
||||
// Then
|
||||
Block {
|
||||
statements: vec![],
|
||||
return_expression: Some((
|
||||
ReturnKind::HardReturn,
|
||||
// return fibonacci(n-1) + fibonacci(n-2)
|
||||
Box::new(Expression(
|
||||
ExpressionKind::BinOp(
|
||||
BinaryOperator::Add,
|
||||
// fibonacci(n-1)
|
||||
Box::new(Expression(
|
||||
ExpressionKind::FunctionCall(FunctionCall {
|
||||
name: fibonacci_name.clone(),
|
||||
return_type: TypeKind::I32,
|
||||
parameters: vec![Expression(
|
||||
ExpressionKind::BinOp(
|
||||
BinaryOperator::Minus,
|
||||
Box::new(Expression(
|
||||
ExpressionKind::Variable(
|
||||
VariableReference(
|
||||
TypeKind::I32,
|
||||
fibonacci_n.clone(),
|
||||
Default::default(),
|
||||
),
|
||||
),
|
||||
Default::default(),
|
||||
)),
|
||||
Box::new(Expression(
|
||||
ExpressionKind::Literal(Literal::I32(
|
||||
1,
|
||||
)),
|
||||
Default::default(),
|
||||
)),
|
||||
),
|
||||
Default::default(),
|
||||
)],
|
||||
}),
|
||||
Default::default(),
|
||||
)),
|
||||
// fibonacci(n-2)
|
||||
Box::new(Expression(
|
||||
ExpressionKind::FunctionCall(FunctionCall {
|
||||
name: fibonacci_name.clone(),
|
||||
return_type: TypeKind::I32,
|
||||
parameters: vec![Expression(
|
||||
ExpressionKind::BinOp(
|
||||
BinaryOperator::Minus,
|
||||
Box::new(Expression(
|
||||
ExpressionKind::Variable(
|
||||
VariableReference(
|
||||
TypeKind::I32,
|
||||
fibonacci_n.clone(),
|
||||
Default::default(),
|
||||
),
|
||||
),
|
||||
Default::default(),
|
||||
)),
|
||||
Box::new(Expression(
|
||||
ExpressionKind::Literal(Literal::I32(
|
||||
2,
|
||||
)),
|
||||
Default::default(),
|
||||
)),
|
||||
),
|
||||
Default::default(),
|
||||
)],
|
||||
}),
|
||||
Default::default(),
|
||||
)),
|
||||
),
|
||||
Default::default(),
|
||||
)),
|
||||
Box::new(Expression(
|
||||
ExprKind::Literal(Literal::I32(2)),
|
||||
Default::default(),
|
||||
)),
|
||||
),
|
||||
Default::default(),
|
||||
)),
|
||||
range: Default::default(),
|
||||
},
|
||||
// No else-block
|
||||
None,
|
||||
// Then
|
||||
Block {
|
||||
statements: vec![],
|
||||
return_expression: Some((
|
||||
ReturnKind::Hard,
|
||||
// return fibonacci(n-1) + fibonacci(n-2)
|
||||
Box::new(Expression(
|
||||
ExprKind::BinOp(
|
||||
BinaryOperator::Add,
|
||||
// fibonacci(n-1)
|
||||
Box::new(Expression(
|
||||
ExprKind::FunctionCall(FunctionCall {
|
||||
name: fibonacci_name.clone(),
|
||||
return_type: TypeKind::I32,
|
||||
parameters: vec![Expression(
|
||||
ExprKind::BinOp(
|
||||
BinaryOperator::Minus,
|
||||
Box::new(Expression(
|
||||
ExprKind::Variable(
|
||||
VariableReference(
|
||||
TypeKind::I32,
|
||||
fibonacci_n.clone(),
|
||||
Default::default(),
|
||||
),
|
||||
),
|
||||
Default::default(),
|
||||
)),
|
||||
Box::new(Expression(
|
||||
ExprKind::Literal(Literal::I32(1)),
|
||||
Default::default(),
|
||||
)),
|
||||
),
|
||||
Default::default(),
|
||||
)],
|
||||
}),
|
||||
Default::default(),
|
||||
)),
|
||||
// fibonacci(n-2)
|
||||
Box::new(Expression(
|
||||
ExprKind::FunctionCall(FunctionCall {
|
||||
name: fibonacci_name.clone(),
|
||||
return_type: TypeKind::I32,
|
||||
parameters: vec![Expression(
|
||||
ExprKind::BinOp(
|
||||
BinaryOperator::Minus,
|
||||
Box::new(Expression(
|
||||
ExprKind::Variable(
|
||||
VariableReference(
|
||||
TypeKind::I32,
|
||||
fibonacci_n.clone(),
|
||||
Default::default(),
|
||||
),
|
||||
),
|
||||
Default::default(),
|
||||
)),
|
||||
Box::new(Expression(
|
||||
ExprKind::Literal(Literal::I32(2)),
|
||||
Default::default(),
|
||||
)),
|
||||
),
|
||||
Default::default(),
|
||||
)],
|
||||
}),
|
||||
Default::default(),
|
||||
)),
|
||||
),
|
||||
Default::default(),
|
||||
)),
|
||||
)),
|
||||
meta: Default::default(),
|
||||
},
|
||||
// No else-block
|
||||
None,
|
||||
)),
|
||||
Default::default(),
|
||||
)),
|
||||
Default::default(),
|
||||
)],
|
||||
// return 1
|
||||
return_expression: Some((
|
||||
ReturnKind::SoftReturn,
|
||||
ReturnKind::Soft,
|
||||
Box::new(Expression(
|
||||
ExpressionKind::Literal(Literal::I32(1)),
|
||||
ExprKind::Literal(Literal::I32(1)),
|
||||
Default::default(),
|
||||
)),
|
||||
)),
|
||||
range: Default::default(),
|
||||
meta: Default::default(),
|
||||
},
|
||||
Default::default(),
|
||||
),
|
||||
@ -132,20 +131,20 @@ fn main() {
|
||||
Block {
|
||||
statements: vec![],
|
||||
return_expression: Some((
|
||||
ReturnKind::SoftReturn,
|
||||
ReturnKind::Soft,
|
||||
Box::new(Expression(
|
||||
ExpressionKind::FunctionCall(FunctionCall {
|
||||
ExprKind::FunctionCall(FunctionCall {
|
||||
name: fibonacci_name.clone(),
|
||||
return_type: TypeKind::I32,
|
||||
parameters: vec![Expression(
|
||||
ExpressionKind::Literal(Literal::I32(5)),
|
||||
ExprKind::Literal(Literal::I32(5)),
|
||||
Default::default(),
|
||||
)],
|
||||
}),
|
||||
Default::default(),
|
||||
)),
|
||||
)),
|
||||
range: Default::default(),
|
||||
meta: Default::default(),
|
||||
},
|
||||
Default::default(),
|
||||
),
|
||||
|
109
reid/src/ast/mod.rs
Normal file
109
reid/src/ast/mod.rs
Normal file
@ -0,0 +1,109 @@
|
||||
use crate::token_stream::TokenRange;
|
||||
|
||||
pub mod parse;
|
||||
pub mod process;
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct Type(pub TypeKind, pub TokenRange);
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum TypeKind {
|
||||
I32,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Literal {
|
||||
I32(i32),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Expression(pub ExpressionKind, pub TokenRange);
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum ExpressionKind {
|
||||
VariableName(String),
|
||||
Literal(Literal),
|
||||
Binop(BinaryOperator, Box<Expression>, Box<Expression>),
|
||||
FunctionCall(Box<FunctionCallExpression>),
|
||||
BlockExpr(Box<Block>),
|
||||
IfExpr(Box<IfExpression>),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum BinaryOperator {
|
||||
Add,
|
||||
Minus,
|
||||
Mult,
|
||||
|
||||
And,
|
||||
LessThan,
|
||||
}
|
||||
|
||||
impl BinaryOperator {
|
||||
pub fn get_precedence(&self) -> i8 {
|
||||
use BinaryOperator::*;
|
||||
match &self {
|
||||
Add => 10,
|
||||
Minus => 10,
|
||||
Mult => 20,
|
||||
And => 100,
|
||||
LessThan => 100,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct FunctionCallExpression(pub String, pub Vec<Expression>, pub TokenRange);
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct IfExpression(pub Expression, pub Block, pub Option<Block>, pub TokenRange);
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct LetStatement(pub String, pub Expression, pub TokenRange);
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ImportStatement(Vec<String>, pub TokenRange);
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct FunctionDefinition(pub FunctionSignature, pub Block, pub TokenRange);
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct FunctionSignature {
|
||||
pub name: String,
|
||||
pub args: Vec<(String, Type)>,
|
||||
pub return_type: Option<Type>,
|
||||
pub range: TokenRange,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum ReturnType {
|
||||
Soft,
|
||||
Hard,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Block(
|
||||
pub Vec<BlockLevelStatement>,
|
||||
pub Option<(ReturnType, Expression)>,
|
||||
pub TokenRange,
|
||||
);
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum BlockLevelStatement {
|
||||
Let(LetStatement),
|
||||
Import(ImportStatement),
|
||||
Expression(Expression),
|
||||
Return(ReturnType, Expression),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum TopLevelStatement {
|
||||
Import(ImportStatement),
|
||||
FunctionDefinition(FunctionDefinition),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Module {
|
||||
pub name: String,
|
||||
pub top_level_statements: Vec<TopLevelStatement>,
|
||||
}
|
@ -1,3 +1,4 @@
|
||||
use crate::ast::*;
|
||||
use crate::{
|
||||
lexer::Token,
|
||||
token_stream::{Error, TokenRange, TokenStream},
|
||||
@ -10,14 +11,6 @@ where
|
||||
fn parse(stream: TokenStream) -> Result<Self, Error>;
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Type(pub TypeKind, pub TokenRange);
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum TypeKind {
|
||||
I32,
|
||||
}
|
||||
|
||||
impl Parse for Type {
|
||||
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
||||
let kind = if let Some(Token::Identifier(ident)) = stream.next() {
|
||||
@ -33,24 +26,6 @@ impl Parse for Type {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Literal {
|
||||
I32(i32),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Expression(pub ExpressionKind, pub TokenRange);
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum ExpressionKind {
|
||||
VariableName(String),
|
||||
Literal(Literal),
|
||||
Binop(BinaryOperator, Box<Expression>, Box<Expression>),
|
||||
FunctionCall(Box<FunctionCallExpression>),
|
||||
BlockExpr(Box<Block>),
|
||||
IfExpr(Box<IfExpression>),
|
||||
}
|
||||
|
||||
impl Parse for Expression {
|
||||
fn parse(mut stream: TokenStream) -> Result<Expression, Error> {
|
||||
let lhs = parse_primary_expression(&mut stream)?;
|
||||
@ -142,16 +117,6 @@ fn parse_binop_rhs(
|
||||
Ok(lhs)
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum BinaryOperator {
|
||||
Add,
|
||||
Minus,
|
||||
Mult,
|
||||
|
||||
And,
|
||||
LessThan,
|
||||
}
|
||||
|
||||
impl Parse for BinaryOperator {
|
||||
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
||||
Ok(match (stream.next(), stream.peek()) {
|
||||
@ -169,22 +134,6 @@ impl Parse for BinaryOperator {
|
||||
}
|
||||
}
|
||||
|
||||
impl BinaryOperator {
|
||||
pub fn get_precedence(&self) -> i8 {
|
||||
use BinaryOperator::*;
|
||||
match &self {
|
||||
Add => 10,
|
||||
Minus => 10,
|
||||
Mult => 20,
|
||||
And => 100,
|
||||
LessThan => 100,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct FunctionCallExpression(pub String, pub Vec<Expression>, pub TokenRange);
|
||||
|
||||
impl Parse for FunctionCallExpression {
|
||||
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
||||
if let Some(Token::Identifier(name)) = stream.next() {
|
||||
@ -213,23 +162,18 @@ impl Parse for FunctionCallExpression {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct IfExpression(pub Expression, pub Block, pub TokenRange);
|
||||
|
||||
impl Parse for IfExpression {
|
||||
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
||||
stream.expect(Token::If)?;
|
||||
Ok(IfExpression(
|
||||
stream.parse()?,
|
||||
stream.parse()?,
|
||||
None,
|
||||
stream.get_range().unwrap(),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct LetStatement(pub String, pub Expression, pub TokenRange);
|
||||
|
||||
impl Parse for LetStatement {
|
||||
fn parse(mut stream: TokenStream) -> Result<LetStatement, Error> {
|
||||
stream.expect(Token::LetKeyword)?;
|
||||
@ -250,9 +194,6 @@ impl Parse for LetStatement {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ImportStatement(Vec<String>, pub TokenRange);
|
||||
|
||||
impl Parse for ImportStatement {
|
||||
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
||||
stream.expect(Token::ImportKeyword)?;
|
||||
@ -278,9 +219,6 @@ impl Parse for ImportStatement {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct FunctionDefinition(pub FunctionSignature, pub Block, pub TokenRange);
|
||||
|
||||
impl Parse for FunctionDefinition {
|
||||
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
||||
stream.expect(Token::FnKeyword)?;
|
||||
@ -292,14 +230,6 @@ impl Parse for FunctionDefinition {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct FunctionSignature {
|
||||
pub name: String,
|
||||
pub args: Vec<(String, Type)>,
|
||||
pub return_type: Option<Type>,
|
||||
pub range: TokenRange,
|
||||
}
|
||||
|
||||
impl Parse for FunctionSignature {
|
||||
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
||||
if let Some(Token::Identifier(name)) = stream.next() {
|
||||
@ -331,19 +261,6 @@ impl Parse for FunctionSignature {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum ReturnType {
|
||||
Soft,
|
||||
Hard,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Block(
|
||||
pub Vec<BlockLevelStatement>,
|
||||
pub Option<(ReturnType, Expression)>,
|
||||
pub TokenRange,
|
||||
);
|
||||
|
||||
impl Parse for Block {
|
||||
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
||||
let mut statements = Vec::new();
|
||||
@ -382,14 +299,6 @@ impl Parse for Block {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum BlockLevelStatement {
|
||||
Let(LetStatement),
|
||||
Import(ImportStatement),
|
||||
Expression(Expression),
|
||||
Return(ReturnType, Expression),
|
||||
}
|
||||
|
||||
impl Parse for BlockLevelStatement {
|
||||
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
||||
use BlockLevelStatement as Stmt;
|
||||
@ -417,12 +326,6 @@ impl Parse for BlockLevelStatement {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum TopLevelStatement {
|
||||
Import(ImportStatement),
|
||||
FunctionDefinition(FunctionDefinition),
|
||||
}
|
||||
|
||||
impl Parse for TopLevelStatement {
|
||||
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
||||
use TopLevelStatement as Stmt;
|
544
reid/src/ast/process.rs
Normal file
544
reid/src/ast/process.rs
Normal file
@ -0,0 +1,544 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::{
|
||||
ast,
|
||||
mir::{self, StmtKind, VariableReference},
|
||||
token_stream::TokenRange,
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum InferredType {
|
||||
FromVariable(String, TokenRange),
|
||||
FunctionReturn(String, TokenRange),
|
||||
Static(mir::TypeKind, TokenRange),
|
||||
OneOf(Vec<InferredType>),
|
||||
Void(TokenRange),
|
||||
DownstreamError(IntoMIRError, TokenRange),
|
||||
}
|
||||
|
||||
fn all_ok<T, E>(result: Vec<Result<T, E>>) -> Option<Vec<T>> {
|
||||
let mut res = Vec::with_capacity(result.len());
|
||||
for item in result {
|
||||
if let Ok(item) = item {
|
||||
res.push(item);
|
||||
} else {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
Some(res)
|
||||
}
|
||||
|
||||
impl InferredType {
|
||||
fn collapse(
|
||||
&self,
|
||||
state: &mut State,
|
||||
scope: &VirtualScope,
|
||||
) -> Result<mir::TypeKind, IntoMIRError> {
|
||||
match self {
|
||||
InferredType::FromVariable(name, token_range) => {
|
||||
if let Some(inferred) = scope.get_var(name) {
|
||||
let temp = inferred.collapse(state, scope);
|
||||
state.note(temp)
|
||||
} else {
|
||||
state.err(IntoMIRError::VariableNotDefined(name.clone(), *token_range))
|
||||
}
|
||||
}
|
||||
InferredType::FunctionReturn(name, token_range) => {
|
||||
if let Some(type_kind) = scope.get_return_type(name) {
|
||||
Ok(*type_kind)
|
||||
} else {
|
||||
state.err(IntoMIRError::VariableNotDefined(name.clone(), *token_range))
|
||||
}
|
||||
}
|
||||
InferredType::Static(type_kind, _) => Ok(*type_kind),
|
||||
InferredType::OneOf(inferred_types) => {
|
||||
let collapsed = all_ok(
|
||||
inferred_types
|
||||
.iter()
|
||||
.map(|t| {
|
||||
let temp = t.collapse(state, scope);
|
||||
state.note(temp)
|
||||
})
|
||||
.collect(),
|
||||
);
|
||||
if let Some(list) = collapsed {
|
||||
if let Some(first) = list.first() {
|
||||
if list.iter().all(|i| i == first) {
|
||||
Ok((*first).into())
|
||||
} else {
|
||||
state.err(IntoMIRError::ConflictingType(self.get_range()))
|
||||
}
|
||||
} else {
|
||||
state.err(IntoMIRError::VoidType(self.get_range()))
|
||||
}
|
||||
} else {
|
||||
state.err(IntoMIRError::DownstreamError(self.get_range()))
|
||||
}
|
||||
}
|
||||
InferredType::Void(token_range) => state.err(IntoMIRError::VoidType(*token_range)),
|
||||
InferredType::DownstreamError(e, _) => state.err(e.clone()),
|
||||
}
|
||||
}
|
||||
|
||||
fn get_range(&self) -> TokenRange {
|
||||
match &self {
|
||||
InferredType::FromVariable(_, token_range) => *token_range,
|
||||
InferredType::FunctionReturn(_, token_range) => *token_range,
|
||||
InferredType::Static(_, token_range) => *token_range,
|
||||
InferredType::OneOf(inferred_types) => {
|
||||
inferred_types.iter().map(|i| i.get_range()).sum()
|
||||
}
|
||||
InferredType::Void(token_range) => *token_range,
|
||||
InferredType::DownstreamError(_, range) => *range,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct VirtualVariable {
|
||||
name: String,
|
||||
inferred: InferredType,
|
||||
meta: mir::Metadata,
|
||||
}
|
||||
|
||||
pub struct VirtualFunctionSignature {
|
||||
name: String,
|
||||
return_type: mir::TypeKind,
|
||||
parameter_types: Vec<mir::TypeKind>,
|
||||
metadata: mir::Metadata,
|
||||
}
|
||||
|
||||
pub enum VirtualStorageError {
|
||||
KeyAlreadyExists(String),
|
||||
}
|
||||
|
||||
pub struct VirtualStorage<T> {
|
||||
storage: HashMap<String, Vec<T>>,
|
||||
}
|
||||
|
||||
impl<T> VirtualStorage<T> {
|
||||
fn set(&mut self, name: String, value: T) -> Result<(), VirtualStorageError> {
|
||||
let result = if let Some(list) = self.storage.get_mut(&name) {
|
||||
list.push(value);
|
||||
Err(VirtualStorageError::KeyAlreadyExists(name.clone()))
|
||||
} else {
|
||||
self.storage.insert(name, vec![value]);
|
||||
Ok(())
|
||||
};
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
fn get(&self, name: &String) -> Option<&T> {
|
||||
if let Some(list) = self.storage.get(name) {
|
||||
list.first()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Default for VirtualStorage<T> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
storage: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum IntoMIRError {
|
||||
DuplicateVariable(String, TokenRange),
|
||||
DuplicateFunction(String, TokenRange),
|
||||
VariableNotDefined(String, TokenRange),
|
||||
FunctionNotDefined(String, TokenRange),
|
||||
DownstreamError(TokenRange),
|
||||
ConflictingType(TokenRange),
|
||||
VoidType(TokenRange),
|
||||
}
|
||||
|
||||
pub struct VirtualScope {
|
||||
variables: VirtualStorage<VirtualVariable>,
|
||||
functions: VirtualStorage<VirtualFunctionSignature>,
|
||||
}
|
||||
|
||||
impl VirtualScope {
|
||||
pub fn set_var(&mut self, variable: VirtualVariable) -> Result<(), IntoMIRError> {
|
||||
let range = variable.meta.range;
|
||||
match self.variables.set(variable.name.clone(), variable) {
|
||||
Ok(_) => Ok(()),
|
||||
Err(VirtualStorageError::KeyAlreadyExists(n)) => {
|
||||
Err(IntoMIRError::DuplicateVariable(n, range))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_fun(&mut self, function: VirtualFunctionSignature) -> Result<(), IntoMIRError> {
|
||||
let range = function.metadata.range;
|
||||
match self.functions.set(function.name.clone(), function) {
|
||||
Ok(_) => Ok(()),
|
||||
Err(VirtualStorageError::KeyAlreadyExists(n)) => {
|
||||
Err(IntoMIRError::DuplicateVariable(n, range))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_var(&self, name: &String) -> Option<&InferredType> {
|
||||
self.variables.get(name).map(|v| &v.inferred)
|
||||
}
|
||||
|
||||
pub fn get_return_type(&self, name: &String) -> Option<&mir::TypeKind> {
|
||||
self.functions.get(name).map(|v| &v.return_type)
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for VirtualScope {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
variables: Default::default(),
|
||||
functions: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct State {
|
||||
errors: Vec<IntoMIRError>,
|
||||
fatal: bool,
|
||||
}
|
||||
|
||||
impl State {
|
||||
fn note<T: std::fmt::Debug>(
|
||||
&mut self,
|
||||
value: Result<T, IntoMIRError>,
|
||||
) -> Result<T, IntoMIRError> {
|
||||
dbg!(&value);
|
||||
if let Err(e) = &value {
|
||||
self.errors.push(e.clone());
|
||||
}
|
||||
value
|
||||
}
|
||||
|
||||
fn err<T>(&mut self, error: IntoMIRError) -> Result<T, IntoMIRError> {
|
||||
self.errors.push(error.clone());
|
||||
Err(error)
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for State {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
errors: Default::default(),
|
||||
fatal: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ast::Module {
|
||||
pub fn process(&self) -> mir::Module {
|
||||
let mut state = State::default();
|
||||
let mut scope = VirtualScope::default();
|
||||
|
||||
for stmt in &self.top_level_statements {
|
||||
match stmt {
|
||||
FunctionDefinition(ast::FunctionDefinition(signature, _, range)) => {
|
||||
state.note(scope.set_fun(VirtualFunctionSignature {
|
||||
name: signature.name.clone(),
|
||||
return_type: signature.return_type.into(),
|
||||
parameter_types: signature.args.iter().map(|p| p.1.into()).collect(),
|
||||
metadata: (*range).into(),
|
||||
}));
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
let mut imports = Vec::new();
|
||||
let mut functions = Vec::new();
|
||||
|
||||
use ast::TopLevelStatement::*;
|
||||
for stmt in &self.top_level_statements {
|
||||
match stmt {
|
||||
Import(import) => {
|
||||
for name in &import.0 {
|
||||
imports.push(mir::Import(name.clone(), import.1.into()));
|
||||
}
|
||||
}
|
||||
FunctionDefinition(ast::FunctionDefinition(signature, block, range)) => {
|
||||
for (name, ptype) in &signature.args {
|
||||
state.note(scope.set_var(VirtualVariable {
|
||||
name: name.clone(),
|
||||
inferred: InferredType::Static((*ptype).into(), *range),
|
||||
meta: ptype.1.into(),
|
||||
}));
|
||||
}
|
||||
|
||||
dbg!(&signature);
|
||||
|
||||
if let Some(mir_block) = block.process(&mut state, &mut scope) {
|
||||
let def = mir::FunctionDefinition {
|
||||
name: signature.name.clone(),
|
||||
parameters: signature
|
||||
.args
|
||||
.iter()
|
||||
.cloned()
|
||||
.map(|p| (p.0, p.1.into()))
|
||||
.collect(),
|
||||
kind: mir::FunctionDefinitionKind::Local(mir_block, (*range).into()),
|
||||
};
|
||||
functions.push(def);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dbg!(&state);
|
||||
|
||||
// TODO do something with state here
|
||||
|
||||
mir::Module {
|
||||
name: self.name.clone(),
|
||||
imports,
|
||||
functions,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ast::Block {
|
||||
pub fn process(&self, state: &mut State, scope: &mut VirtualScope) -> Option<mir::Block> {
|
||||
let mut mir_statements = Vec::new();
|
||||
|
||||
for statement in &self.0 {
|
||||
let (kind, range): (Option<mir::StmtKind>, TokenRange) = match statement {
|
||||
ast::BlockLevelStatement::Let(s_let) => {
|
||||
let res = s_let.1.infer_return_type().collapse(state, scope);
|
||||
let collapsed = state.note(res);
|
||||
let inferred = match &collapsed {
|
||||
Ok(t) => InferredType::Static(*t, s_let.2),
|
||||
Err(e) => InferredType::DownstreamError(e.clone(), s_let.2),
|
||||
};
|
||||
state
|
||||
.note(scope.set_var(VirtualVariable {
|
||||
name: s_let.0.clone(),
|
||||
inferred,
|
||||
meta: s_let.2.into(),
|
||||
}))
|
||||
.ok();
|
||||
|
||||
(
|
||||
collapsed.ok().and_then(|t| {
|
||||
s_let.1.process(state, scope).map(|e| {
|
||||
mir::StmtKind::Let(
|
||||
mir::VariableReference(t, s_let.0.clone(), s_let.2.into()),
|
||||
e,
|
||||
)
|
||||
})
|
||||
}),
|
||||
s_let.2,
|
||||
)
|
||||
}
|
||||
ast::BlockLevelStatement::Import(_) => todo!(),
|
||||
ast::BlockLevelStatement::Expression(e) => (
|
||||
e.process(state, scope).map(|e| StmtKind::Expression(e)),
|
||||
e.1,
|
||||
),
|
||||
ast::BlockLevelStatement::Return(_, e) => (
|
||||
e.process(state, scope).map(|e| StmtKind::Expression(e)),
|
||||
e.1,
|
||||
),
|
||||
};
|
||||
|
||||
if let Some(kind) = kind {
|
||||
mir_statements.push(mir::Statement(kind, range.into()));
|
||||
} else {
|
||||
state.fatal = true;
|
||||
}
|
||||
}
|
||||
|
||||
let return_expression = if let Some(r) = &self.1 {
|
||||
if let Some(expr) = r.1.process(state, scope) {
|
||||
Some((r.0.into(), Box::new(expr)))
|
||||
} else {
|
||||
state.fatal = true;
|
||||
None?
|
||||
}
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
Some(mir::Block {
|
||||
statements: mir_statements,
|
||||
return_expression,
|
||||
meta: self.2.into(),
|
||||
})
|
||||
}
|
||||
|
||||
fn infer_return_type(&self) -> InferredType {
|
||||
self.1
|
||||
.as_ref()
|
||||
.map(|(_, expr)| expr.infer_return_type())
|
||||
.unwrap_or(InferredType::Void(self.2))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ast::ReturnType> for mir::ReturnKind {
|
||||
fn from(value: ast::ReturnType) -> Self {
|
||||
match value {
|
||||
ast::ReturnType::Soft => mir::ReturnKind::Soft,
|
||||
ast::ReturnType::Hard => mir::ReturnKind::Hard,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ast::Expression {
|
||||
fn process(&self, state: &mut State, scope: &mut VirtualScope) -> Option<mir::Expression> {
|
||||
let kind = match &self.0 {
|
||||
ast::ExpressionKind::VariableName(name) => {
|
||||
let ty = scope.get_var(name);
|
||||
if let Some(ty) = ty {
|
||||
let res = ty.collapse(state, scope);
|
||||
state
|
||||
.note(res)
|
||||
.map(|result| {
|
||||
mir::ExprKind::Variable(VariableReference(
|
||||
result,
|
||||
name.clone(),
|
||||
self.1.into(),
|
||||
))
|
||||
})
|
||||
.ok()
|
||||
} else {
|
||||
state
|
||||
.err(IntoMIRError::VariableNotDefined(
|
||||
name.clone(),
|
||||
self.1.into(),
|
||||
))
|
||||
.ok()
|
||||
}
|
||||
}
|
||||
ast::ExpressionKind::Literal(literal) => Some(mir::ExprKind::Literal(literal.mir())),
|
||||
ast::ExpressionKind::Binop(binary_operator, lhs, rhs) => {
|
||||
let mir_lhs = lhs.process(state, scope);
|
||||
let mir_rhs = rhs.process(state, scope);
|
||||
Some(mir::ExprKind::BinOp(
|
||||
binary_operator.mir(),
|
||||
Box::new(mir_lhs?),
|
||||
Box::new(mir_rhs?),
|
||||
))
|
||||
}
|
||||
ast::ExpressionKind::FunctionCall(fn_call_expr) => {
|
||||
if let Some(fn_type) = scope.get_return_type(&fn_call_expr.0).cloned() {
|
||||
let parameters = all_ok(
|
||||
fn_call_expr
|
||||
.1
|
||||
.iter()
|
||||
.map(|e| {
|
||||
e.process(state, scope)
|
||||
.ok_or(IntoMIRError::DownstreamError(self.1.into()))
|
||||
})
|
||||
.collect(),
|
||||
);
|
||||
if let Some(parameters) = parameters {
|
||||
Some(mir::ExprKind::FunctionCall(mir::FunctionCall {
|
||||
name: fn_call_expr.0.clone(),
|
||||
return_type: fn_type,
|
||||
parameters,
|
||||
}))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
state
|
||||
.err(IntoMIRError::FunctionNotDefined(
|
||||
fn_call_expr.0.clone(),
|
||||
self.1,
|
||||
))
|
||||
.ok()
|
||||
}
|
||||
}
|
||||
ast::ExpressionKind::BlockExpr(block) => {
|
||||
block.process(state, scope).map(|b| mir::ExprKind::Block(b))
|
||||
}
|
||||
ast::ExpressionKind::IfExpr(if_expression) => {
|
||||
let cond = if_expression.0.process(state, scope);
|
||||
let then_block = if_expression.1.process(state, scope);
|
||||
let else_block = if let Some(el) = &if_expression.2 {
|
||||
Some(el.process(state, scope)?)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
Some(mir::ExprKind::If(mir::IfExpression(
|
||||
Box::new(cond?),
|
||||
then_block?,
|
||||
else_block,
|
||||
)))
|
||||
}
|
||||
};
|
||||
|
||||
kind.map(|k| mir::Expression(k, self.1.into()))
|
||||
}
|
||||
|
||||
fn infer_return_type(&self) -> InferredType {
|
||||
use ast::ExpressionKind::*;
|
||||
match &self.0 {
|
||||
VariableName(name) => InferredType::FromVariable(name.clone(), self.1),
|
||||
Literal(lit) => InferredType::Static(lit.mir().as_type(), self.1),
|
||||
Binop(_, lhs, rhs) => {
|
||||
InferredType::OneOf(vec![lhs.infer_return_type(), rhs.infer_return_type()])
|
||||
}
|
||||
FunctionCall(fncall) => InferredType::FunctionReturn(fncall.0.clone(), self.1),
|
||||
BlockExpr(block) => block.infer_return_type(),
|
||||
IfExpr(exp) => {
|
||||
let mut types = vec![exp.1.infer_return_type()];
|
||||
if let Some(e) = &exp.2 {
|
||||
types.push(e.infer_return_type())
|
||||
}
|
||||
InferredType::OneOf(types)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ast::BinaryOperator {
|
||||
fn mir(&self) -> mir::BinaryOperator {
|
||||
match self {
|
||||
ast::BinaryOperator::Add => mir::BinaryOperator::Add,
|
||||
ast::BinaryOperator::Minus => mir::BinaryOperator::Minus,
|
||||
ast::BinaryOperator::Mult => mir::BinaryOperator::Mult,
|
||||
ast::BinaryOperator::And => mir::BinaryOperator::And,
|
||||
ast::BinaryOperator::LessThan => {
|
||||
mir::BinaryOperator::Logic(mir::LogicOperator::LessThan)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ast::Literal {
|
||||
fn mir(&self) -> mir::Literal {
|
||||
match *self {
|
||||
ast::Literal::I32(v) => mir::Literal::I32(v),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ast::TypeKind> for mir::TypeKind {
|
||||
fn from(value: ast::TypeKind) -> Self {
|
||||
match value {
|
||||
ast::TypeKind::I32 => mir::TypeKind::I32,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ast::Type> for mir::TypeKind {
|
||||
fn from(value: ast::Type) -> Self {
|
||||
value.0.into()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Option<ast::Type>> for mir::TypeKind {
|
||||
fn from(value: Option<ast::Type>) -> Self {
|
||||
match value {
|
||||
Some(v) => v.into(),
|
||||
None => mir::TypeKind::Void,
|
||||
}
|
||||
}
|
||||
}
|
@ -102,17 +102,14 @@ impl<'ctx> Scope<'ctx> {
|
||||
impl mir::Statement {
|
||||
pub fn codegen<'ctx>(&self, scope: &mut Scope<'ctx>) -> Option<Value<'ctx>> {
|
||||
match &self.0 {
|
||||
mir::StatementKind::Let(VariableReference(_, name, _), expression) => {
|
||||
mir::StmtKind::Let(VariableReference(_, name, _), expression) => {
|
||||
let value = expression.codegen(scope).unwrap();
|
||||
scope.stack_values.insert(name.clone(), value);
|
||||
None
|
||||
}
|
||||
mir::StatementKind::If(if_expression) => if_expression.codegen(scope),
|
||||
mir::StatementKind::Import(_) => todo!(),
|
||||
mir::StatementKind::Expression(expression) => {
|
||||
let value = expression.codegen(scope).unwrap();
|
||||
Some(value)
|
||||
}
|
||||
// mir::StmtKind::If(if_expression) => if_expression.codegen(scope),
|
||||
mir::StmtKind::Import(_) => todo!(),
|
||||
mir::StmtKind::Expression(expression) => expression.codegen(scope),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -178,15 +175,15 @@ impl mir::IfExpression {
|
||||
impl mir::Expression {
|
||||
pub fn codegen<'ctx>(&self, scope: &mut Scope<'ctx>) -> Option<Value<'ctx>> {
|
||||
match &self.0 {
|
||||
mir::ExpressionKind::Variable(varref) => {
|
||||
mir::ExprKind::Variable(varref) => {
|
||||
let v = scope
|
||||
.stack_values
|
||||
.get(&varref.1)
|
||||
.expect("Variable reference not found?!");
|
||||
Some(v.clone())
|
||||
}
|
||||
mir::ExpressionKind::Literal(lit) => Some(lit.codegen(scope.context)),
|
||||
mir::ExpressionKind::BinOp(binop, lhs_exp, rhs_exp) => {
|
||||
mir::ExprKind::Literal(lit) => Some(lit.codegen(scope.context)),
|
||||
mir::ExprKind::BinOp(binop, lhs_exp, rhs_exp) => {
|
||||
let lhs = lhs_exp.codegen(scope).expect("lhs has no return value");
|
||||
let rhs = rhs_exp.codegen(scope).expect("rhs has no return value");
|
||||
Some(match binop {
|
||||
@ -203,7 +200,7 @@ impl mir::Expression {
|
||||
}
|
||||
})
|
||||
}
|
||||
mir::ExpressionKind::FunctionCall(call) => {
|
||||
mir::ExprKind::FunctionCall(call) => {
|
||||
let params = call
|
||||
.parameters
|
||||
.iter()
|
||||
@ -215,8 +212,8 @@ impl mir::Expression {
|
||||
.expect("function not found!");
|
||||
Some(scope.block.call(callee, params, "call").unwrap())
|
||||
}
|
||||
mir::ExpressionKind::If(if_expression) => if_expression.codegen(scope),
|
||||
mir::ExpressionKind::Block(block) => {
|
||||
mir::ExprKind::If(if_expression) => if_expression.codegen(scope),
|
||||
mir::ExprKind::Block(block) => {
|
||||
let mut inner_scope = scope.with_block(scope.function.block("inner"));
|
||||
if let Some(ret) = block.codegen(&mut inner_scope) {
|
||||
inner_scope.block.br(&scope.block);
|
||||
@ -249,11 +246,11 @@ impl mir::Block {
|
||||
if let Some((kind, expr)) = &self.return_expression {
|
||||
let ret = expr.codegen(&mut scope).unwrap();
|
||||
match kind {
|
||||
mir::ReturnKind::HardReturn => {
|
||||
mir::ReturnKind::Hard => {
|
||||
scope.block.ret(&ret).unwrap();
|
||||
None
|
||||
}
|
||||
mir::ReturnKind::SoftReturn => Some(ret),
|
||||
mir::ReturnKind::Soft => Some(ret),
|
||||
}
|
||||
} else {
|
||||
None
|
||||
@ -276,6 +273,7 @@ impl TypeKind {
|
||||
match &self {
|
||||
TypeKind::I32 => TypeEnum::Integer(context.type_i32()),
|
||||
TypeKind::I16 => TypeEnum::Integer(context.type_i16()),
|
||||
TypeKind::Void => panic!("Void not a supported type"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,13 +1,11 @@
|
||||
use old_codegen::{form_context, from_statements};
|
||||
use reid_lib::Context;
|
||||
|
||||
use crate::{lexer::Token, parser::TopLevelStatement, token_stream::TokenStream};
|
||||
use crate::{ast::TopLevelStatement, lexer::Token, token_stream::TokenStream};
|
||||
|
||||
mod ast;
|
||||
mod codegen;
|
||||
mod lexer;
|
||||
pub mod mir;
|
||||
mod old_codegen;
|
||||
mod parser;
|
||||
// mod llvm_ir;
|
||||
pub mod codegen;
|
||||
mod token_stream;
|
||||
|
||||
// TODO:
|
||||
@ -41,8 +39,21 @@ pub fn compile(source: &str) -> Result<String, ReidError> {
|
||||
statements.push(statement);
|
||||
}
|
||||
|
||||
let mut context = form_context();
|
||||
let mut module = from_statements(&mut context, statements).unwrap();
|
||||
let text = module.print_to_string().unwrap();
|
||||
Ok(text.to_owned())
|
||||
let ast_module = ast::Module {
|
||||
name: "test".to_owned(),
|
||||
top_level_statements: statements,
|
||||
};
|
||||
|
||||
dbg!(&ast_module);
|
||||
let mir_module = ast_module.process();
|
||||
|
||||
dbg!(&mir_module);
|
||||
|
||||
let mut context = Context::new();
|
||||
let cogegen_module = mir_module.codegen(&mut context);
|
||||
|
||||
Ok(match cogegen_module.module.print_to_string() {
|
||||
Ok(v) => v,
|
||||
Err(e) => panic!("Err: {:?}", e),
|
||||
})
|
||||
}
|
||||
|
@ -1,18 +1,34 @@
|
||||
/// In this module are defined structs that are used for performing passes on
|
||||
/// Reid. It contains a simplified version of Reid which must already be
|
||||
/// type-checked beforehand.
|
||||
use std::collections::HashMap;
|
||||
|
||||
use types::*;
|
||||
|
||||
use crate::token_stream::TokenRange;
|
||||
|
||||
pub mod types;
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct Metadata {
|
||||
pub range: TokenRange,
|
||||
}
|
||||
|
||||
impl From<TokenRange> for Metadata {
|
||||
fn from(value: TokenRange) -> Self {
|
||||
Metadata { range: value }
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Metadata {
|
||||
fn default() -> Self {
|
||||
Metadata {
|
||||
range: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum TypeKind {
|
||||
I32,
|
||||
I16,
|
||||
Void,
|
||||
}
|
||||
|
||||
impl TypeKind {
|
||||
@ -23,14 +39,14 @@ impl TypeKind {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum Literal {
|
||||
I32(i32),
|
||||
I16(i16),
|
||||
}
|
||||
|
||||
impl Literal {
|
||||
fn as_type(self: &Literal) -> TypeKind {
|
||||
pub fn as_type(self: &Literal) -> TypeKind {
|
||||
match self {
|
||||
Literal::I32(_) => TypeKind::I32,
|
||||
Literal::I16(_) => TypeKind::I16,
|
||||
@ -55,15 +71,18 @@ pub enum LogicOperator {
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum ReturnKind {
|
||||
HardReturn,
|
||||
SoftReturn,
|
||||
Hard,
|
||||
Soft,
|
||||
}
|
||||
|
||||
pub struct VariableReference(pub TypeKind, pub String, pub TokenRange);
|
||||
#[derive(Debug)]
|
||||
pub struct VariableReference(pub TypeKind, pub String, pub Metadata);
|
||||
|
||||
pub struct Import(pub String, pub TokenRange);
|
||||
#[derive(Debug)]
|
||||
pub struct Import(pub String, pub Metadata);
|
||||
|
||||
pub enum ExpressionKind {
|
||||
#[derive(Debug)]
|
||||
pub enum ExprKind {
|
||||
Variable(VariableReference),
|
||||
Literal(Literal),
|
||||
BinOp(BinaryOperator, Box<Expression>, Box<Expression>),
|
||||
@ -72,47 +91,55 @@ pub enum ExpressionKind {
|
||||
Block(Block),
|
||||
}
|
||||
|
||||
pub struct Expression(pub ExpressionKind, pub TokenRange);
|
||||
#[derive(Debug)]
|
||||
pub struct Expression(pub ExprKind, pub Metadata);
|
||||
|
||||
/// Condition, Then, Else
|
||||
#[derive(Debug)]
|
||||
pub struct IfExpression(pub Box<Expression>, pub Block, pub Option<Block>);
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct FunctionCall {
|
||||
pub name: String,
|
||||
pub return_type: TypeKind,
|
||||
pub parameters: Vec<Expression>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct FunctionDefinition {
|
||||
pub name: String,
|
||||
pub parameters: Vec<(String, TypeKind)>,
|
||||
pub kind: FunctionDefinitionKind,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum FunctionDefinitionKind {
|
||||
/// Actual definition block and surrounding signature range
|
||||
Local(Block, TokenRange),
|
||||
Local(Block, Metadata),
|
||||
/// Return Type
|
||||
Extern(TypeKind),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Block {
|
||||
/// List of non-returning statements
|
||||
pub statements: Vec<Statement>,
|
||||
pub return_expression: Option<(ReturnKind, Box<Expression>)>,
|
||||
pub range: TokenRange,
|
||||
pub meta: Metadata,
|
||||
}
|
||||
|
||||
pub struct Statement(pub StatementKind, pub TokenRange);
|
||||
#[derive(Debug)]
|
||||
pub struct Statement(pub StmtKind, pub Metadata);
|
||||
|
||||
pub enum StatementKind {
|
||||
#[derive(Debug)]
|
||||
pub enum StmtKind {
|
||||
/// Variable name+type, evaluation
|
||||
Let(VariableReference, Expression),
|
||||
If(IfExpression),
|
||||
Import(Import),
|
||||
Expression(Expression),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Module {
|
||||
pub name: String,
|
||||
pub imports: Vec<Import>,
|
||||
|
@ -16,26 +16,25 @@ impl ReturnType for Block {
|
||||
fn return_type(&self) -> Result<TypeKind, ReturnTypeOther> {
|
||||
self.return_expression
|
||||
.as_ref()
|
||||
.ok_or(ReturnTypeOther::NoBlockReturn(self.range.clone()))
|
||||
.ok_or(ReturnTypeOther::NoBlockReturn(self.meta.range))
|
||||
.and_then(|(_, stmt)| stmt.return_type())
|
||||
}
|
||||
}
|
||||
|
||||
impl ReturnType for Statement {
|
||||
fn return_type(&self) -> Result<TypeKind, ReturnTypeOther> {
|
||||
use StatementKind::*;
|
||||
use StmtKind::*;
|
||||
match &self.0 {
|
||||
Expression(e) => e.return_type(),
|
||||
If(e) => e.return_type(),
|
||||
Import(_) => Err(ReturnTypeOther::Import(self.1)),
|
||||
Let(_, _) => Err(ReturnTypeOther::Let(self.1)),
|
||||
Import(_) => Err(ReturnTypeOther::Import(self.1.range)),
|
||||
Let(_, _) => Err(ReturnTypeOther::Let(self.1.range)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ReturnType for Expression {
|
||||
fn return_type(&self) -> Result<TypeKind, ReturnTypeOther> {
|
||||
use ExpressionKind::*;
|
||||
use ExprKind::*;
|
||||
match &self.0 {
|
||||
Literal(lit) => Ok(lit.as_type()),
|
||||
Variable(var) => var.return_type(),
|
||||
|
@ -1,287 +0,0 @@
|
||||
use std::borrow::BorrowMut;
|
||||
use std::ffi::{CStr, CString};
|
||||
use std::mem;
|
||||
use std::ptr::null_mut;
|
||||
|
||||
use llvm_sys::analysis::LLVMVerifyModule;
|
||||
use llvm_sys::transforms::pass_manager_builder::{
|
||||
self, LLVMOpaquePassManagerBuilder, LLVMPassManagerBuilderCreate,
|
||||
LLVMPassManagerBuilderSetOptLevel,
|
||||
};
|
||||
use llvm_sys::{
|
||||
core::*, prelude::*, LLVMBasicBlock, LLVMBuilder, LLVMContext, LLVMModule, LLVMType, LLVMValue,
|
||||
};
|
||||
|
||||
use crate::parser;
|
||||
|
||||
fn into_cstring<T: Into<String>>(value: T) -> CString {
|
||||
let string = value.into();
|
||||
unsafe { CString::from_vec_with_nul_unchecked((string + "\0").into_bytes()) }
|
||||
}
|
||||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
pub enum Error {
|
||||
#[error("Type mismatch: {0:?} vs {1:?}")]
|
||||
TypeMismatch(IRType, IRType),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
pub enum IRType {
|
||||
I32,
|
||||
Boolean,
|
||||
}
|
||||
|
||||
impl IRType {
|
||||
fn in_context(&self, context: &IRContext) -> *mut LLVMType {
|
||||
use IRType::*;
|
||||
unsafe {
|
||||
return match self {
|
||||
I32 => LLVMInt32TypeInContext(context.context),
|
||||
Boolean => LLVMInt1TypeInContext(context.context),
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct IRValue(pub IRType, *mut LLVMValue);
|
||||
|
||||
impl IRValue {
|
||||
pub fn from_literal(literal: &parser::Literal, module: &IRModule) -> Self {
|
||||
use parser::Literal;
|
||||
match literal {
|
||||
Literal::I32(v) => {
|
||||
let ir_type = IRType::I32;
|
||||
unsafe {
|
||||
let ir_value = LLVMConstInt(
|
||||
ir_type.in_context(module.context),
|
||||
mem::transmute(*v as i64),
|
||||
1,
|
||||
);
|
||||
return IRValue(ir_type, ir_value);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
pub struct IRContext {
|
||||
context: *mut LLVMContext,
|
||||
builder: *mut LLVMBuilder,
|
||||
}
|
||||
|
||||
impl IRContext {
|
||||
pub fn new() -> IRContext {
|
||||
unsafe {
|
||||
// Set up a context, module and builder in that context.
|
||||
let context = LLVMContextCreate();
|
||||
let builder = LLVMCreateBuilderInContext(context);
|
||||
IRContext { context, builder }
|
||||
}
|
||||
}
|
||||
|
||||
pub fn module<'a>(&'a mut self, name: String) -> IRModule<'a> {
|
||||
IRModule::new(self, name)
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for IRContext {
|
||||
fn drop(&mut self) {
|
||||
// Clean up. Values created in the context mostly get cleaned up there.
|
||||
unsafe {
|
||||
LLVMDisposeBuilder(self.builder);
|
||||
LLVMContextDispose(self.context);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct IRModule<'a> {
|
||||
context: &'a mut IRContext,
|
||||
module: *mut LLVMModule,
|
||||
}
|
||||
|
||||
impl<'a> IRModule<'a> {
|
||||
fn new<'b: 'a>(context: &'b mut IRContext, name: String) -> IRModule<'a> {
|
||||
unsafe {
|
||||
let module =
|
||||
LLVMModuleCreateWithNameInContext(into_cstring(name).as_ptr(), context.context);
|
||||
|
||||
IRModule { context, module }
|
||||
}
|
||||
}
|
||||
|
||||
pub fn print_to_string(&mut self) -> Result<&str, std::str::Utf8Error> {
|
||||
unsafe {
|
||||
LLVMVerifyModule(
|
||||
self.module,
|
||||
llvm_sys::analysis::LLVMVerifierFailureAction::LLVMPrintMessageAction,
|
||||
null_mut(),
|
||||
);
|
||||
CStr::from_ptr(LLVMPrintModuleToString(self.module)).to_str()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Drop for IRModule<'a> {
|
||||
fn drop(&mut self) {
|
||||
// Clean up. Values created in the context mostly get cleaned up there.
|
||||
unsafe {
|
||||
LLVMDisposeModule(self.module);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct IRFunction<'a, 'b> {
|
||||
pub module: &'b IRModule<'a>,
|
||||
/// The actual function
|
||||
value: *mut LLVMValue,
|
||||
}
|
||||
|
||||
impl<'a, 'b> IRFunction<'a, 'b> {
|
||||
pub fn new(name: &String, module: &'b IRModule<'a>) -> IRFunction<'a, 'b> {
|
||||
unsafe {
|
||||
// TODO, fix later!
|
||||
|
||||
let return_type = LLVMInt32TypeInContext(module.context.context);
|
||||
let mut argts = [];
|
||||
let func_type =
|
||||
LLVMFunctionType(return_type, argts.as_mut_ptr(), argts.len() as u32, 0);
|
||||
|
||||
let function = LLVMAddFunction(module.module, into_cstring(name).as_ptr(), func_type);
|
||||
|
||||
IRFunction {
|
||||
module,
|
||||
value: function,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct IRBlock<'a, 'b, 'c> {
|
||||
pub function: &'c IRFunction<'a, 'b>,
|
||||
blockref: *mut LLVMBasicBlock,
|
||||
}
|
||||
|
||||
impl<'a, 'b, 'c> IRBlock<'a, 'b, 'c> {
|
||||
pub fn new(function: &'c IRFunction<'a, 'b>, name: &CStr) -> IRBlock<'a, 'b, 'c> {
|
||||
unsafe {
|
||||
let blockref =
|
||||
LLVMCreateBasicBlockInContext(function.module.context.context, name.as_ptr());
|
||||
|
||||
IRBlock { function, blockref }
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add(
|
||||
&mut self,
|
||||
IRValue(lhs_t, lhs_v): IRValue,
|
||||
IRValue(rhs_t, rhs_v): IRValue,
|
||||
) -> Result<IRValue, Error> {
|
||||
unsafe {
|
||||
LLVMPositionBuilderAtEnd(self.function.module.context.builder, self.blockref);
|
||||
if lhs_t == rhs_t {
|
||||
Ok(IRValue(
|
||||
lhs_t,
|
||||
LLVMBuildAdd(
|
||||
self.function.module.context.builder,
|
||||
lhs_v,
|
||||
rhs_v,
|
||||
c"tmpadd".as_ptr(),
|
||||
),
|
||||
))
|
||||
} else {
|
||||
Err(Error::TypeMismatch(lhs_t, rhs_t))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn mult(
|
||||
&mut self,
|
||||
IRValue(lhs_t, lhs_v): IRValue,
|
||||
IRValue(rhs_t, rhs_v): IRValue,
|
||||
) -> Result<IRValue, Error> {
|
||||
unsafe {
|
||||
LLVMPositionBuilderAtEnd(self.function.module.context.builder, self.blockref);
|
||||
if lhs_t == rhs_t {
|
||||
Ok(IRValue(
|
||||
lhs_t,
|
||||
LLVMBuildMul(
|
||||
self.function.module.context.builder,
|
||||
lhs_v,
|
||||
rhs_v,
|
||||
c"tmpadd".as_ptr(),
|
||||
),
|
||||
))
|
||||
} else {
|
||||
Err(Error::TypeMismatch(lhs_t, rhs_t))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn less_than(
|
||||
&mut self,
|
||||
IRValue(lhs_t, lhs_v): IRValue,
|
||||
IRValue(rhs_t, rhs_v): IRValue,
|
||||
) -> Result<IRValue, Error> {
|
||||
unsafe {
|
||||
LLVMPositionBuilderAtEnd(self.function.module.context.builder, self.blockref);
|
||||
if lhs_t == rhs_t {
|
||||
Ok(IRValue(
|
||||
IRType::Boolean,
|
||||
LLVMBuildICmp(
|
||||
self.function.module.context.builder,
|
||||
llvm_sys::LLVMIntPredicate::LLVMIntULT,
|
||||
lhs_v,
|
||||
rhs_v,
|
||||
c"IntULT".as_ptr(),
|
||||
),
|
||||
))
|
||||
} else {
|
||||
Err(Error::TypeMismatch(lhs_t, rhs_t))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_return(&mut self, value: Option<IRValue>) {
|
||||
unsafe {
|
||||
LLVMPositionBuilderAtEnd(self.function.module.context.builder, self.blockref);
|
||||
if let Some(IRValue(_, value)) = value {
|
||||
LLVMBuildRet(self.function.module.context.builder, value);
|
||||
} else {
|
||||
LLVMBuildRetVoid(self.function.module.context.builder);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn branch(
|
||||
&mut self,
|
||||
IRValue(_, condition): IRValue,
|
||||
then_block: &mut IRBlock,
|
||||
else_block: &mut IRBlock,
|
||||
) {
|
||||
unsafe {
|
||||
LLVMPositionBuilderAtEnd(self.function.module.context.builder, self.blockref);
|
||||
LLVMBuildCondBr(
|
||||
self.function.module.context.builder,
|
||||
condition,
|
||||
then_block.blockref,
|
||||
else_block.blockref,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn move_into(&mut self, block: &mut IRBlock) {
|
||||
unsafe {
|
||||
LLVMPositionBuilderAtEnd(self.function.module.context.builder, self.blockref);
|
||||
LLVMBuildBr(self.function.module.context.builder, block.blockref);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'b, 'c> Drop for IRBlock<'a, 'b, 'c> {
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
LLVMAppendExistingBasicBlock(self.function.value, self.blockref);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,176 +0,0 @@
|
||||
mod llvm;
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use llvm::{Error, IRBlock, IRContext, IRFunction, IRModule, IRValue};
|
||||
|
||||
use crate::{
|
||||
parser::{
|
||||
Block, BlockLevelStatement, Expression, ExpressionKind, FunctionDefinition, IfExpression,
|
||||
LetStatement, ReturnType,
|
||||
},
|
||||
TopLevelStatement,
|
||||
};
|
||||
|
||||
pub fn form_context() -> IRContext {
|
||||
IRContext::new()
|
||||
}
|
||||
|
||||
pub fn from_statements(
|
||||
context: &mut IRContext,
|
||||
statements: Vec<TopLevelStatement>,
|
||||
) -> Result<IRModule, Error> {
|
||||
let mut module = context.module("testmod".to_owned());
|
||||
let mut scope = ScopeData::new();
|
||||
for statement in statements {
|
||||
statement.codegen(&mut scope, &mut module);
|
||||
}
|
||||
|
||||
Ok(module)
|
||||
}
|
||||
|
||||
impl TopLevelStatement {
|
||||
fn codegen(&self, scope: &mut ScopeData, module: &mut IRModule) {
|
||||
match self {
|
||||
Self::FunctionDefinition(func) => func.codegen(scope, module),
|
||||
Self::Import(_) => panic!("not implemented"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl FunctionDefinition {
|
||||
fn codegen(&self, scope: &mut ScopeData, module: &mut IRModule) {
|
||||
let FunctionDefinition(signature, block, _) = self;
|
||||
let ir_function = IRFunction::new(&signature.name, module);
|
||||
|
||||
let ir_block = IRBlock::new(&ir_function, c"entry");
|
||||
let mut scope = scope.inner(ir_block);
|
||||
if let Some((_, val)) = block.codegen(&mut scope) {
|
||||
scope.block.add_return(Some(val));
|
||||
} else {
|
||||
scope.block.add_return(None);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Block {
|
||||
#[must_use]
|
||||
fn codegen(&self, scope: &mut Scope) -> Option<(ReturnType, IRValue)> {
|
||||
for statement in &self.0 {
|
||||
statement.codegen(scope);
|
||||
}
|
||||
|
||||
if let Some((ret_type, return_exp)) = &self.1 {
|
||||
let value = return_exp.codegen(scope);
|
||||
Some((*ret_type, value))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl BlockLevelStatement {
|
||||
fn codegen(&self, scope: &mut Scope) {
|
||||
use BlockLevelStatement::*;
|
||||
match self {
|
||||
Expression(exp) | Return(ReturnType::Soft, exp) => {
|
||||
exp.codegen(scope);
|
||||
}
|
||||
Let(LetStatement(name, exp, _)) => {
|
||||
let val = exp.codegen(scope);
|
||||
scope.data.insert(name, val);
|
||||
}
|
||||
Return(ReturnType::Hard, _) => panic!("hard returns here should not be possible.."),
|
||||
Import(_) => panic!("block level import not supported"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Expression {
|
||||
fn codegen(&self, scope: &mut Scope) -> IRValue {
|
||||
let Expression(kind, _) = self;
|
||||
|
||||
use ExpressionKind::*;
|
||||
match kind {
|
||||
Literal(lit) => IRValue::from_literal(lit, &scope.block.function.module),
|
||||
VariableName(v) => scope.data.fetch(v),
|
||||
Binop(op, lhs, rhs) => {
|
||||
let lhs = lhs.codegen(scope);
|
||||
let rhs = rhs.codegen(scope);
|
||||
use crate::parser::BinaryOperator::*;
|
||||
match op {
|
||||
Add => scope.block.add(lhs, rhs).unwrap(),
|
||||
Mult => scope.block.mult(lhs, rhs).unwrap(),
|
||||
LessThan => scope.block.less_than(lhs, rhs).unwrap(),
|
||||
_ => panic!("operator not supported: {:?}", op),
|
||||
}
|
||||
}
|
||||
IfExpr(ifx) => {
|
||||
let IfExpression(expr, block, _) = ifx.as_ref();
|
||||
let condition = expr.codegen(scope);
|
||||
|
||||
let mut thenb = IRBlock::new(scope.block.function, c"then");
|
||||
let mut afterb = IRBlock::new(scope.block.function, c"merge");
|
||||
|
||||
scope.block.branch(condition, &mut thenb, &mut afterb);
|
||||
scope.block = afterb;
|
||||
|
||||
let mut then = scope.inner(thenb);
|
||||
match block.codegen(&mut then) {
|
||||
Some((ReturnType::Hard, v)) => then.block.add_return(Some(v)),
|
||||
_ => then.block.move_into(&mut scope.block),
|
||||
}
|
||||
|
||||
IRValue::from_literal(&crate::parser::Literal::I32(1), scope.block.function.module)
|
||||
}
|
||||
BlockExpr(_) => panic!("block expr not supported"),
|
||||
FunctionCall(_) => panic!("function call expr not supported"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct ScopeData {
|
||||
vars: HashMap<String, IRValue>,
|
||||
}
|
||||
|
||||
impl ScopeData {
|
||||
fn new() -> ScopeData {
|
||||
ScopeData {
|
||||
vars: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn with_block<'a, 'b, 'c>(self, block: IRBlock<'a, 'b, 'c>) -> Scope<'a, 'b, 'c> {
|
||||
Scope { data: self, block }
|
||||
}
|
||||
|
||||
fn inner<'a, 'b, 'c>(&self, block: IRBlock<'a, 'b, 'c>) -> Scope<'a, 'b, 'c> {
|
||||
self.clone().with_block(block)
|
||||
}
|
||||
|
||||
fn fetch(&self, name: &String) -> IRValue {
|
||||
match self.vars.get(name) {
|
||||
Some(val) => val.clone(),
|
||||
_ => panic!("No such variable in scope: {}", name),
|
||||
}
|
||||
}
|
||||
|
||||
fn insert(&mut self, name: &String, value: IRValue) {
|
||||
match self.vars.insert(name.clone(), value) {
|
||||
Some(_) => panic!("{} was already defined in scope", name),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct Scope<'a, 'b, 'c> {
|
||||
data: ScopeData,
|
||||
block: IRBlock<'a, 'b, 'c>,
|
||||
}
|
||||
|
||||
impl<'a, 'b, 'c> Scope<'a, 'b, 'c> {
|
||||
fn inner(&self, block: IRBlock<'a, 'b, 'c>) -> Scope<'a, 'b, 'c> {
|
||||
self.data.clone().with_block(block)
|
||||
}
|
||||
}
|
@ -1,222 +0,0 @@
|
||||
use std::collections::{hash_map, HashMap};
|
||||
|
||||
use crate::{
|
||||
ast::{
|
||||
BinaryOperator, Block, BlockLevelStatement, Expression, FunctionCallExpression,
|
||||
FunctionDefinition, FunctionSignature, ReturnType, TopLevelStatement,
|
||||
},
|
||||
llvm_ir::{self, IRBlock, IRFunction, IRModule, IRValue, IRValueType},
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ScopeData {
|
||||
named_vars: HashMap<String, IRValue>,
|
||||
defined_functions: HashMap<String, (FunctionSignature, Option<IRFunction>)>,
|
||||
}
|
||||
|
||||
impl ScopeData {
|
||||
pub fn inner<'a, 'b>(&self, block: &'b mut IRBlock<'a>) -> Scope<'a, 'b> {
|
||||
Scope {
|
||||
block,
|
||||
data: self.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn var(&self, name: &String) -> Option<&IRValue> {
|
||||
self.named_vars.get(name)
|
||||
}
|
||||
|
||||
pub fn set_var(&mut self, name: &str, val: IRValue) -> Result<(), Error> {
|
||||
if let hash_map::Entry::Vacant(e) = self.named_vars.entry(name.to_owned()) {
|
||||
e.insert(val);
|
||||
Ok(())
|
||||
} else {
|
||||
Err(Error::VariableAlreadyDefined(name.to_owned()))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn function(
|
||||
&mut self,
|
||||
name: &String,
|
||||
) -> Option<&mut (FunctionSignature, Option<IRFunction>)> {
|
||||
self.defined_functions.get_mut(name)
|
||||
}
|
||||
|
||||
pub fn set_function_signature(
|
||||
&mut self,
|
||||
name: &str,
|
||||
sig: FunctionSignature,
|
||||
ir: IRFunction,
|
||||
) -> Result<(), Error> {
|
||||
if let hash_map::Entry::Vacant(e) = self.defined_functions.entry(name.to_owned()) {
|
||||
e.insert((sig, Some(ir)));
|
||||
Ok(())
|
||||
} else {
|
||||
Err(Error::VariableAlreadyDefined(name.to_owned()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Scope<'a, 'b> {
|
||||
pub block: &'b mut IRBlock<'a>,
|
||||
pub data: ScopeData,
|
||||
}
|
||||
|
||||
impl<'a, 'b> Scope<'a, 'b> {
|
||||
pub fn inner<'c>(&'c mut self) -> Scope<'a, 'c> {
|
||||
Scope {
|
||||
block: self.block,
|
||||
data: self.data.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn codegen_from_statements(statements: Vec<TopLevelStatement>) -> Result<IRModule, Error> {
|
||||
let mut module = IRModule::new("testmod");
|
||||
|
||||
let mut scope = ScopeData {
|
||||
defined_functions: HashMap::new(),
|
||||
named_vars: HashMap::new(),
|
||||
};
|
||||
|
||||
for statement in &statements {
|
||||
match statement {
|
||||
TopLevelStatement::FunctionDefinition(FunctionDefinition(sig, _)) => {
|
||||
let function = module.create_func(&sig.name, IRValueType::I32);
|
||||
scope.set_function_signature(&sig.name.clone(), sig.clone(), function)?;
|
||||
}
|
||||
TopLevelStatement::Import(_) => {}
|
||||
}
|
||||
}
|
||||
|
||||
for statement in &statements {
|
||||
statement.codegen(&mut module, &mut scope)?;
|
||||
}
|
||||
|
||||
Ok(module)
|
||||
}
|
||||
|
||||
impl TopLevelStatement {
|
||||
pub fn codegen(&self, module: &mut IRModule, root_data: &mut ScopeData) -> Result<(), Error> {
|
||||
match self {
|
||||
TopLevelStatement::FunctionDefinition(FunctionDefinition(sig, block)) => {
|
||||
if let Some((_, ir)) = root_data.function(&sig.name) {
|
||||
if let Some(ir_function) = ir.take() {
|
||||
let mut ir_block = module.create_block();
|
||||
let mut scope = root_data.inner(&mut ir_block);
|
||||
|
||||
let (_, value) = match block.codegen(&mut scope)? {
|
||||
Some(v) => v,
|
||||
None => panic!("Void-return type function not yet implemented!"),
|
||||
};
|
||||
|
||||
ir_function.add_definition(value, ir_block);
|
||||
} else {
|
||||
Err(Error::FunctionAlreadyDefined(sig.name.clone()))?
|
||||
}
|
||||
} else {
|
||||
panic!("Function was not declared before it's definition")
|
||||
}
|
||||
}
|
||||
TopLevelStatement::Import(_) => {}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Block {
|
||||
pub fn codegen(&self, scope: &mut Scope) -> Result<Option<(ReturnType, IRValue)>, Error> {
|
||||
for statement in &self.0 {
|
||||
statement.codegen(scope)?;
|
||||
}
|
||||
|
||||
let value = if let Some((rt, exp)) = &self.1 {
|
||||
Some((*rt, exp.codegen(scope)?))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
Ok(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl BlockLevelStatement {
|
||||
pub fn codegen(&self, scope: &mut Scope) -> Result<(), Error> {
|
||||
match self {
|
||||
BlockLevelStatement::Let(let_statement) => {
|
||||
let val = let_statement.1.codegen(scope)?;
|
||||
scope.data.set_var(&let_statement.0, val)?;
|
||||
Ok(())
|
||||
}
|
||||
BlockLevelStatement::Return(_) => panic!("Should never happen"),
|
||||
BlockLevelStatement::Import(_) => Ok(()), // TODO: To implement
|
||||
BlockLevelStatement::Expression(e) => {
|
||||
let _value = e.codegen(scope)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Expression {
|
||||
pub fn codegen(&self, scope: &mut Scope) -> Result<IRValue, Error> {
|
||||
use Expression::*;
|
||||
match self {
|
||||
Binop(op, lhs, rhs) => match op {
|
||||
BinaryOperator::Add => {
|
||||
let lhs = lhs.codegen(scope)?;
|
||||
let rhs = rhs.codegen(scope)?;
|
||||
Ok(scope.block.add(lhs, rhs)?)
|
||||
}
|
||||
BinaryOperator::Mult => {
|
||||
let lhs = lhs.codegen(scope)?;
|
||||
let rhs = rhs.codegen(scope)?;
|
||||
Ok(scope.block.mul(lhs, rhs)?)
|
||||
}
|
||||
_ => panic!("Other binary operators not supported yet!"),
|
||||
},
|
||||
BlockExpr(block) => {
|
||||
let mut inner = scope.inner();
|
||||
|
||||
Ok(match block.codegen(&mut inner)? {
|
||||
Some((r_type, value)) => match r_type {
|
||||
ReturnType::Soft => value,
|
||||
ReturnType::Hard => {
|
||||
panic!("Hard returns in inner blocks not supported yet")
|
||||
}
|
||||
},
|
||||
None => panic!("Void-return type block not yet implemented!"),
|
||||
})
|
||||
}
|
||||
FunctionCall(fc) => {
|
||||
let FunctionCallExpression(name, _) = &**fc;
|
||||
if let Some((sig, _)) = scope.data.function(name) {
|
||||
Ok(scope.block.function_call(sig)?)
|
||||
} else {
|
||||
Err(Error::UndefinedFunction(name.clone()))?
|
||||
}
|
||||
}
|
||||
VariableName(name) => scope
|
||||
.data
|
||||
.var(name)
|
||||
.cloned()
|
||||
.ok_or(Error::UndefinedVariable(name.clone())),
|
||||
Literal(lit) => Ok(scope.block.get_const(lit)),
|
||||
IfExpr(_) => panic!("if expressions not yet supported"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
pub enum Error {
|
||||
#[error("Variable '{0}' already defined")]
|
||||
VariableAlreadyDefined(String),
|
||||
#[error("Variable '{0}' not yet defined")]
|
||||
UndefinedVariable(String),
|
||||
#[error("Function '{0}' not defined")]
|
||||
UndefinedFunction(String),
|
||||
#[error("Function '{0}' already defined")]
|
||||
FunctionAlreadyDefined(String),
|
||||
#[error(transparent)]
|
||||
Deeper(#[from] llvm_ir::Error),
|
||||
}
|
@ -1,203 +0,0 @@
|
||||
use std::ffi::{CStr, CString};
|
||||
use std::mem;
|
||||
|
||||
use llvm_sys::{core::*, prelude::*, LLVMBuilder, LLVMContext, LLVMModule};
|
||||
|
||||
use crate::ast::{FunctionSignature, Literal};
|
||||
|
||||
macro_rules! cstr {
|
||||
($string:expr) => {
|
||||
core::ffi::CStr::from_bytes_with_nul_unchecked(concat!($string, "\0").as_bytes()).as_ptr()
|
||||
};
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
#[must_use = "value contains raw pointer and must be inserted somewhere"]
|
||||
pub struct IRValue(IRValueType, LLVMValueRef);
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub enum IRValueType {
|
||||
I32,
|
||||
}
|
||||
|
||||
impl IRValueType {
|
||||
unsafe fn get_llvm_type(&self, module: &mut IRModule) -> LLVMTypeRef {
|
||||
match *self {
|
||||
Self::I32 => LLVMInt32TypeInContext(module.context),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn into_cstring<T: Into<String>>(value: T) -> CString {
|
||||
let string = value.into();
|
||||
unsafe { CString::from_vec_with_nul_unchecked((string + "\0").into_bytes()) }
|
||||
}
|
||||
|
||||
pub struct IRModule {
|
||||
context: *mut LLVMContext,
|
||||
module: *mut LLVMModule,
|
||||
builder: *mut LLVMBuilder,
|
||||
}
|
||||
|
||||
impl IRModule {
|
||||
pub fn new<T: Into<String>>(name: T) -> IRModule {
|
||||
unsafe {
|
||||
// Set up a context, module and builder in that context.
|
||||
let context = LLVMContextCreate();
|
||||
let module = LLVMModuleCreateWithNameInContext(into_cstring(name).as_ptr(), context);
|
||||
let builder = LLVMCreateBuilderInContext(context);
|
||||
|
||||
IRModule {
|
||||
context,
|
||||
module,
|
||||
builder,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn create_block(&mut self) -> IRBlock {
|
||||
IRBlock::create("entry", self)
|
||||
}
|
||||
|
||||
pub fn create_func<T: Into<String>>(
|
||||
&mut self,
|
||||
name: T,
|
||||
return_type: IRValueType,
|
||||
) -> IRFunction {
|
||||
unsafe {
|
||||
let mut argts = [];
|
||||
let func_type = LLVMFunctionType(
|
||||
return_type.get_llvm_type(self),
|
||||
argts.as_mut_ptr(),
|
||||
argts.len() as u32,
|
||||
0,
|
||||
);
|
||||
|
||||
let anon_func = LLVMAddFunction(self.module, into_cstring(name).as_ptr(), func_type);
|
||||
IRFunction {
|
||||
value: IRValue(return_type, anon_func),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn print_to_string(&mut self) -> Result<&str, std::str::Utf8Error> {
|
||||
unsafe { CStr::from_ptr(LLVMPrintModuleToString(self.module)).to_str() }
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for IRModule {
|
||||
fn drop(&mut self) {
|
||||
// Clean up. Values created in the context mostly get cleaned up there.
|
||||
unsafe {
|
||||
LLVMDisposeBuilder(self.builder);
|
||||
LLVMDisposeModule(self.module);
|
||||
LLVMContextDispose(self.context);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct IRFunction {
|
||||
value: IRValue,
|
||||
}
|
||||
|
||||
impl IRFunction {
|
||||
pub fn add_definition(self, ret: IRValue, block: IRBlock) {
|
||||
unsafe {
|
||||
LLVMAppendExistingBasicBlock(self.value.1, block.blockref);
|
||||
LLVMBuildRet(block.module.builder, ret.1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct IRBlock<'a> {
|
||||
module: &'a mut IRModule,
|
||||
blockref: LLVMBasicBlockRef,
|
||||
}
|
||||
|
||||
impl<'a> IRBlock<'a> {
|
||||
fn create<T: Into<String>>(name: T, codegen: &'a mut IRModule) -> IRBlock<'a> {
|
||||
unsafe {
|
||||
let blockref =
|
||||
LLVMCreateBasicBlockInContext(codegen.context, into_cstring(name).as_ptr());
|
||||
LLVMPositionBuilderAtEnd(codegen.builder, blockref);
|
||||
IRBlock {
|
||||
module: codegen,
|
||||
blockref,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_const(&mut self, literal_type: &Literal) -> IRValue {
|
||||
unsafe {
|
||||
match *literal_type {
|
||||
Literal::I32(v) => IRValue(
|
||||
IRValueType::I32,
|
||||
LLVMConstInt(
|
||||
LLVMInt32TypeInContext(self.module.context),
|
||||
mem::transmute(v as i64),
|
||||
1,
|
||||
),
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add(&mut self, lhs: IRValue, rhs: IRValue) -> Result<IRValue, Error> {
|
||||
unsafe {
|
||||
if lhs.0 == rhs.0 {
|
||||
Ok(IRValue(
|
||||
lhs.0,
|
||||
LLVMBuildAdd(self.module.builder, lhs.1, rhs.1, cstr!("tmpadd")),
|
||||
))
|
||||
} else {
|
||||
Err(Error::TypeMismatch(lhs.0, rhs.0))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn mul(&mut self, lhs: IRValue, rhs: IRValue) -> Result<IRValue, Error> {
|
||||
unsafe {
|
||||
if lhs.0 == rhs.0 {
|
||||
Ok(IRValue(
|
||||
lhs.0,
|
||||
LLVMBuildMul(self.module.builder, lhs.1, rhs.1, cstr!("tmpadd")),
|
||||
))
|
||||
} else {
|
||||
Err(Error::TypeMismatch(lhs.0, rhs.0))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn function_call(&mut self, callee: &FunctionSignature) -> Result<IRValue, Error> {
|
||||
unsafe {
|
||||
let function = LLVMGetNamedFunction(
|
||||
self.module.module,
|
||||
into_cstring(callee.name.clone()).as_ptr(),
|
||||
);
|
||||
|
||||
let ret_t = LLVMInt32TypeInContext(self.module.context);
|
||||
let mut argts = [];
|
||||
let mut args = [];
|
||||
|
||||
let fun_t = LLVMFunctionType(ret_t, argts.as_mut_ptr(), argts.len() as u32, 0);
|
||||
|
||||
let call = LLVMBuildCall2(
|
||||
self.module.builder,
|
||||
fun_t,
|
||||
function,
|
||||
args.as_mut_ptr(),
|
||||
args.len() as u32,
|
||||
into_cstring(&callee.name).as_ptr(),
|
||||
);
|
||||
|
||||
Ok(IRValue(IRValueType::I32, call))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum Error {
|
||||
#[error("Type Mismatch: {0:?} {1:?}")]
|
||||
TypeMismatch(IRValueType, IRValueType),
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
use crate::{
|
||||
ast::parse::Parse,
|
||||
lexer::{FullToken, Position, Token},
|
||||
parser::Parse,
|
||||
};
|
||||
|
||||
pub struct TokenStream<'a, 'b> {
|
||||
@ -177,6 +177,27 @@ impl Default for TokenRange {
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::Add for TokenRange {
|
||||
type Output = TokenRange;
|
||||
|
||||
fn add(self, rhs: Self) -> Self::Output {
|
||||
TokenRange {
|
||||
start: self.start.min(rhs.start),
|
||||
end: self.end.min(rhs.end),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::iter::Sum for TokenRange {
|
||||
fn sum<I: Iterator<Item = Self>>(mut iter: I) -> Self {
|
||||
let mut start = iter.next().unwrap_or(Default::default());
|
||||
for item in iter {
|
||||
start = start + item;
|
||||
}
|
||||
start
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
pub enum Error {
|
||||
#[error("Expected {} at Ln {}, Col {}, got {:?}", .0, (.2).1, (.2).0, .1)]
|
||||
|
Loading…
Reference in New Issue
Block a user