Add soft/hard returns
This commit is contained in:
parent
f22505be91
commit
413cd87a02
@ -1,6 +1,6 @@
|
|||||||
use reid::compile;
|
use reid::compile;
|
||||||
|
|
||||||
pub static EASIEST: &str = include_str!("./reid/easy.reid");
|
pub static EASIEST: &str = include_str!("./reid/easiest.reid");
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let text = match compile(EASIEST) {
|
let text = match compile(EASIEST) {
|
||||||
|
@ -7,8 +7,9 @@ fn somethingelse() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let hello = 32 + 2 + 4;
|
let hello = 32 + {
|
||||||
let beep =
|
2 + somethingelse()
|
||||||
hello + 100 + somethingelse();
|
};
|
||||||
|
let beep = hello + 5;
|
||||||
return beep;
|
return beep;
|
||||||
}
|
}
|
38
src/ast.rs
38
src/ast.rs
@ -201,19 +201,42 @@ impl Parse for FunctionSignature {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
pub enum ReturnType {
|
||||||
|
Soft,
|
||||||
|
Hard,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Block(pub Vec<BlockLevelStatement>, pub Option<Expression>);
|
pub struct Block(
|
||||||
|
pub Vec<BlockLevelStatement>,
|
||||||
|
pub Option<(ReturnType, Expression)>,
|
||||||
|
);
|
||||||
|
|
||||||
impl Parse for Block {
|
impl Parse for Block {
|
||||||
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
||||||
let mut statements = Vec::new();
|
let mut statements = Vec::new();
|
||||||
let mut return_stmt = None;
|
let mut return_stmt = None;
|
||||||
stream.expect(Token::BraceOpen)?;
|
stream.expect(Token::BraceOpen)?;
|
||||||
|
|
||||||
while !matches!(stream.peek(), Some(Token::BraceClose)) {
|
while !matches!(stream.peek(), Some(Token::BraceClose)) {
|
||||||
|
if let Some((r_type, e)) = return_stmt.take() {
|
||||||
|
println!("Oh no, does this statement lack ;");
|
||||||
|
dbg!(r_type, e);
|
||||||
|
}
|
||||||
let statement = stream.parse()?;
|
let statement = stream.parse()?;
|
||||||
if let BlockLevelStatement::Return(e) = &statement {
|
if let BlockLevelStatement::Return((r_type, e)) = &statement {
|
||||||
return_stmt = Some(e.clone());
|
match r_type {
|
||||||
|
ReturnType::Hard => {
|
||||||
|
return_stmt = Some((*r_type, e.clone()));
|
||||||
break; // Return has to be the last statement
|
break; // Return has to be the last statement
|
||||||
|
// TODO: Make a mechanism that "can" parse even after this
|
||||||
|
}
|
||||||
|
ReturnType::Soft => {
|
||||||
|
return_stmt = Some((*r_type, e.clone()));
|
||||||
|
continue; // In theory possible to have lines after a soft return
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
statements.push(statement);
|
statements.push(statement);
|
||||||
}
|
}
|
||||||
@ -227,7 +250,7 @@ pub enum BlockLevelStatement {
|
|||||||
Let(LetStatement),
|
Let(LetStatement),
|
||||||
Import(ImportStatement),
|
Import(ImportStatement),
|
||||||
Expression(Expression),
|
Expression(Expression),
|
||||||
Return(Expression),
|
Return((ReturnType, Expression)),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Parse for BlockLevelStatement {
|
impl Parse for BlockLevelStatement {
|
||||||
@ -240,12 +263,15 @@ impl Parse for BlockLevelStatement {
|
|||||||
stream.next();
|
stream.next();
|
||||||
let exp = stream.parse()?;
|
let exp = stream.parse()?;
|
||||||
stream.expect(Token::Semi)?;
|
stream.expect(Token::Semi)?;
|
||||||
Stmt::Return(exp)
|
Stmt::Return((ReturnType::Hard, exp))
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
if let Ok(e) = stream.parse() {
|
if let Ok(e) = stream.parse() {
|
||||||
stream.expect(Token::Semi)?;
|
if stream.expect(Token::Semi).is_ok() {
|
||||||
Stmt::Expression(e)
|
Stmt::Expression(e)
|
||||||
|
} else {
|
||||||
|
Stmt::Return((ReturnType::Soft, e))
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
Err(stream.expected_err("expression")?)?
|
Err(stream.expected_err("expression")?)?
|
||||||
}
|
}
|
||||||
|
@ -2,8 +2,8 @@ use std::collections::{hash_map, HashMap};
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
ast::{
|
ast::{
|
||||||
BinaryOperator, BlockLevelStatement, Expression, FunctionCallExpression,
|
BinaryOperator, Block, BlockLevelStatement, Expression, FunctionCallExpression,
|
||||||
FunctionDefinition, FunctionSignature, TopLevelStatement,
|
FunctionDefinition, FunctionSignature, ReturnType, TopLevelStatement,
|
||||||
},
|
},
|
||||||
llvm_ir::{self, IRBlock, IRFunction, IRModule, IRValue, IRValueType},
|
llvm_ir::{self, IRBlock, IRFunction, IRModule, IRValue, IRValueType},
|
||||||
};
|
};
|
||||||
@ -15,8 +15,11 @@ pub struct ScopeData {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl ScopeData {
|
impl ScopeData {
|
||||||
pub fn inner<'a>(&'a mut self, block: IRBlock<'a>) -> Scope {
|
pub fn inner<'a, 'b>(&self, block: &'b mut IRBlock<'a>) -> Scope<'a, 'b> {
|
||||||
Scope { block, data: self }
|
Scope {
|
||||||
|
block,
|
||||||
|
data: self.clone(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn var(&self, name: &String) -> Option<&IRValue> {
|
pub fn var(&self, name: &String) -> Option<&IRValue> {
|
||||||
@ -55,8 +58,17 @@ impl ScopeData {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub struct Scope<'a, 'b> {
|
pub struct Scope<'a, 'b> {
|
||||||
pub block: IRBlock<'a>,
|
pub block: &'b mut IRBlock<'a>,
|
||||||
pub data: &'b mut ScopeData,
|
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> {
|
pub fn codegen_from_statements(statements: Vec<TopLevelStatement>) -> Result<IRModule, Error> {
|
||||||
@ -89,19 +101,16 @@ impl TopLevelStatement {
|
|||||||
match self {
|
match self {
|
||||||
TopLevelStatement::FunctionDefinition(FunctionDefinition(sig, block)) => {
|
TopLevelStatement::FunctionDefinition(FunctionDefinition(sig, block)) => {
|
||||||
if let Some((_, ir)) = root_data.function(&sig.name) {
|
if let Some((_, ir)) = root_data.function(&sig.name) {
|
||||||
if let Some(ir) = ir.take() {
|
if let Some(ir_function) = ir.take() {
|
||||||
let mut scope = root_data.inner(module.create_block());
|
let mut ir_block = module.create_block();
|
||||||
|
let mut scope = root_data.inner(&mut ir_block);
|
||||||
|
|
||||||
for statement in &block.0 {
|
let (_, value) = match block.codegen(&mut scope)? {
|
||||||
statement.codegen(&mut scope)?;
|
Some(v) => v,
|
||||||
}
|
None => panic!("Void-return type function not yet implemented!"),
|
||||||
|
|
||||||
let value = if let Some(exp) = &block.1 {
|
|
||||||
exp.codegen(&mut scope)?
|
|
||||||
} else {
|
|
||||||
panic!("Void-return type function not yet implemented!");
|
|
||||||
};
|
};
|
||||||
ir.add_definition(value, scope.block);
|
|
||||||
|
ir_function.add_definition(value, ir_block);
|
||||||
} else {
|
} else {
|
||||||
Err(Error::FunctionAlreadyDefined(sig.name.clone()))?
|
Err(Error::FunctionAlreadyDefined(sig.name.clone()))?
|
||||||
}
|
}
|
||||||
@ -115,6 +124,22 @@ impl TopLevelStatement {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 {
|
impl BlockLevelStatement {
|
||||||
pub fn codegen(&self, scope: &mut Scope) -> Result<(), Error> {
|
pub fn codegen(&self, scope: &mut Scope) -> Result<(), Error> {
|
||||||
match self {
|
match self {
|
||||||
@ -146,7 +171,19 @@ impl Expression {
|
|||||||
Ok(scope.block.mul(lhs, rhs)?)
|
Ok(scope.block.mul(lhs, rhs)?)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
BlockExpr(_) => panic!("(BlockExpr) Not implemented!"),
|
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) => {
|
FunctionCall(fc) => {
|
||||||
let FunctionCallExpression(name, _) = &**fc;
|
let FunctionCallExpression(name, _) = &**fc;
|
||||||
if let Some((sig, _)) = scope.data.function(name) {
|
if let Some((sig, _)) = scope.data.function(name) {
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
ast::TopLevelStatement, codegen::codegen_from_statements, lexer::Token, llvm_ir::IRModule,
|
ast::TopLevelStatement, codegen::codegen_from_statements, lexer::Token,
|
||||||
token_stream::TokenStream,
|
token_stream::TokenStream,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -15,6 +15,19 @@ macro_rules! cstr {
|
|||||||
#[must_use = "value contains raw pointer and must be inserted somewhere"]
|
#[must_use = "value contains raw pointer and must be inserted somewhere"]
|
||||||
pub struct IRValue(IRValueType, LLVMValueRef);
|
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 {
|
fn into_cstring<T: Into<String>>(value: T) -> CString {
|
||||||
let string = value.into();
|
let string = value.into();
|
||||||
unsafe { CString::from_vec_with_nul_unchecked((string + "\0").into_bytes()) }
|
unsafe { CString::from_vec_with_nul_unchecked((string + "\0").into_bytes()) }
|
||||||
@ -83,19 +96,6 @@ impl Drop for IRModule {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
||||||
pub enum IRValueType {
|
|
||||||
I32,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl IRValueType {
|
|
||||||
unsafe fn get_llvm_type(&self, codegen: &mut IRModule) -> LLVMTypeRef {
|
|
||||||
match *self {
|
|
||||||
Self::I32 => LLVMInt32TypeInContext(codegen.context),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct IRFunction {
|
pub struct IRFunction {
|
||||||
value: IRValue,
|
value: IRValue,
|
||||||
|
Loading…
Reference in New Issue
Block a user