code formatting

This commit is contained in:
pca006132 2020-12-30 09:40:56 +08:00
parent b98117a049
commit 03712e3762
4 changed files with 372 additions and 192 deletions

View File

@ -23,13 +23,17 @@ pub fn parse_expr(ctx: &GlobalContext, sym_table: &SymTable, expr: &Expression)
ExpressionType::Binop { a, b, op } => parse_bin_ops(ctx, sym_table, op, a, b), ExpressionType::Binop { a, b, op } => parse_bin_ops(ctx, sym_table, op, a, b),
ExpressionType::Unop { op, a } => parse_unary_ops(ctx, sym_table, op, a), ExpressionType::Unop { op, a } => parse_unary_ops(ctx, sym_table, op, a),
ExpressionType::Compare { vals, ops } => parse_compare(ctx, sym_table, vals, ops), ExpressionType::Compare { vals, ops } => parse_compare(ctx, sym_table, vals, ops),
ExpressionType::Call { args, function, keywords} => { ExpressionType::Call {
args,
function,
keywords,
} => {
if keywords.len() > 0 { if keywords.len() > 0 {
Err("keyword is not supported".into()) Err("keyword is not supported".into())
} else { } else {
parse_call(ctx, sym_table, &args, &function) parse_call(ctx, sym_table, &args, &function)
} }
}, }
ExpressionType::Subscript { a, b } => parse_subscript(ctx, sym_table, a, b), ExpressionType::Subscript { a, b } => parse_subscript(ctx, sym_table, a, b),
ExpressionType::IfExpression { test, body, orelse } => { ExpressionType::IfExpression { test, body, orelse } => {
parse_if_expr(ctx, sym_table, &test, &body, orelse) parse_if_expr(ctx, sym_table, &test, &body, orelse)
@ -356,7 +360,8 @@ fn parse_list_comprehension(
let boolean = PrimitiveType(BOOL_TYPE); let boolean = PrimitiveType(BOOL_TYPE);
for test in comprehension.ifs.iter() { for test in comprehension.ifs.iter() {
let result = parse_expr(ctx, &local_sym, test)?.ok_or("no value in test".to_string())?; let result =
parse_expr(ctx, &local_sym, test)?.ok_or("no value in test".to_string())?;
if result.as_ref() != &boolean { if result.as_ref() != &boolean {
return Err("test must be bool".into()); return Err("test must be bool".into());
} }
@ -366,4 +371,3 @@ fn parse_list_comprehension(
Err("iteration is supported for list only".into()) Err("iteration is supported for list only".into())
} }
} }

View File

@ -1,34 +1,33 @@
extern crate num_bigint;
extern crate inkwell; extern crate inkwell;
extern crate num_bigint;
extern crate rustpython_parser; extern crate rustpython_parser;
pub mod expression;
pub mod inference; pub mod inference;
mod operators;
pub mod primitives; pub mod primitives;
pub mod typedef; pub mod typedef;
pub mod expression;
mod operators;
use std::collections::HashMap;
use std::error::Error; use std::error::Error;
use std::fmt; use std::fmt;
use std::path::Path; use std::path::Path;
use std::collections::HashMap;
use num_traits::cast::ToPrimitive; use num_traits::cast::ToPrimitive;
use rustpython_parser::ast; use rustpython_parser::ast;
use inkwell::OptimizationLevel; use inkwell::basic_block;
use inkwell::builder::Builder; use inkwell::builder::Builder;
use inkwell::context::Context; use inkwell::context::Context;
use inkwell::module::Module; use inkwell::module::Module;
use inkwell::passes;
use inkwell::targets::*; use inkwell::targets::*;
use inkwell::types; use inkwell::types;
use inkwell::types::BasicType; use inkwell::types::BasicType;
use inkwell::values; use inkwell::values;
use inkwell::{IntPredicate, FloatPredicate}; use inkwell::OptimizationLevel;
use inkwell::basic_block; use inkwell::{FloatPredicate, IntPredicate};
use inkwell::passes;
#[derive(Debug)] #[derive(Debug)]
enum CompileErrorKind { enum CompileErrorKind {
@ -38,26 +37,25 @@ enum CompileErrorKind {
IncompatibleTypes, IncompatibleTypes,
UnboundIdentifier, UnboundIdentifier,
BreakOutsideLoop, BreakOutsideLoop,
Internal(&'static str) Internal(&'static str),
} }
impl fmt::Display for CompileErrorKind { impl fmt::Display for CompileErrorKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self { match self {
CompileErrorKind::Unsupported(feature) CompileErrorKind::Unsupported(feature) => write!(
=> write!(f, "The following Python feature is not supported by NAC3: {}", feature), f,
CompileErrorKind::MissingTypeAnnotation "The following Python feature is not supported by NAC3: {}",
=> write!(f, "Missing type annotation"), feature
CompileErrorKind::UnknownTypeAnnotation ),
=> write!(f, "Unknown type annotation"), CompileErrorKind::MissingTypeAnnotation => write!(f, "Missing type annotation"),
CompileErrorKind::IncompatibleTypes CompileErrorKind::UnknownTypeAnnotation => write!(f, "Unknown type annotation"),
=> write!(f, "Incompatible types"), CompileErrorKind::IncompatibleTypes => write!(f, "Incompatible types"),
CompileErrorKind::UnboundIdentifier CompileErrorKind::UnboundIdentifier => write!(f, "Unbound identifier"),
=> write!(f, "Unbound identifier"), CompileErrorKind::BreakOutsideLoop => write!(f, "Break outside loop"),
CompileErrorKind::BreakOutsideLoop CompileErrorKind::Internal(details) => {
=> write!(f, "Break outside loop"), write!(f, "Internal compiler error: {}", details)
CompileErrorKind::Internal(details) }
=> write!(f, "Internal compiler error: {}", details),
} }
} }
} }
@ -108,7 +106,9 @@ impl<'ctx> CodeGen<'ctx> {
module.add_function("output", fn_type, None); module.add_function("output", fn_type, None);
CodeGen { CodeGen {
context, module, pass_manager, context,
module,
pass_manager,
builder: context.create_builder(), builder: context.create_builder(),
current_source_location: ast::Location::default(), current_source_location: ast::Location::default(),
namespace: HashMap::new(), namespace: HashMap::new(),
@ -123,7 +123,7 @@ impl<'ctx> CodeGen<'ctx> {
fn compile_error(&self, kind: CompileErrorKind) -> CompileError { fn compile_error(&self, kind: CompileErrorKind) -> CompileError {
CompileError { CompileError {
location: self.current_source_location, location: self.current_source_location,
kind kind,
} }
} }
@ -134,7 +134,7 @@ impl<'ctx> CodeGen<'ctx> {
"int64" => Ok(self.context.i64_type().into()), "int64" => Ok(self.context.i64_type().into()),
"float32" => Ok(self.context.f32_type().into()), "float32" => Ok(self.context.f32_type().into()),
"float64" => Ok(self.context.f64_type().into()), "float64" => Ok(self.context.f64_type().into()),
_ => Err(self.compile_error(CompileErrorKind::UnknownTypeAnnotation)) _ => Err(self.compile_error(CompileErrorKind::UnknownTypeAnnotation)),
} }
} }
@ -148,37 +148,53 @@ impl<'ctx> CodeGen<'ctx> {
is_async: bool, is_async: bool,
) -> CompileResult<values::FunctionValue<'ctx>> { ) -> CompileResult<values::FunctionValue<'ctx>> {
if is_async { if is_async {
return Err(self.compile_error(CompileErrorKind::Unsupported("async functions"))) return Err(self.compile_error(CompileErrorKind::Unsupported("async functions")));
} }
for decorator in decorator_list.iter() { for decorator in decorator_list.iter() {
self.set_source_location(decorator.location); self.set_source_location(decorator.location);
if let ast::ExpressionType::Identifier { name } = &decorator.node { if let ast::ExpressionType::Identifier { name } = &decorator.node {
if name != "kernel" && name != "portable" { if name != "kernel" && name != "portable" {
return Err(self.compile_error(CompileErrorKind::Unsupported("custom decorators"))) return Err(
self.compile_error(CompileErrorKind::Unsupported("custom decorators"))
);
} }
} else { } else {
return Err(self.compile_error(CompileErrorKind::Unsupported("decorator must be an identifier"))) return Err(self.compile_error(CompileErrorKind::Unsupported(
"decorator must be an identifier",
)));
} }
} }
let args_type = args.args.iter().map(|val| { let args_type = args
.args
.iter()
.map(|val| {
self.set_source_location(val.location); self.set_source_location(val.location);
if let Some(annotation) = &val.annotation { if let Some(annotation) = &val.annotation {
if let ast::ExpressionType::Identifier { name } = &annotation.node { if let ast::ExpressionType::Identifier { name } = &annotation.node {
Ok(self.get_basic_type(&name)?) Ok(self.get_basic_type(&name)?)
} else { } else {
Err(self.compile_error(CompileErrorKind::Unsupported("type annotation must be an identifier"))) Err(self.compile_error(CompileErrorKind::Unsupported(
"type annotation must be an identifier",
)))
} }
} else { } else {
Err(self.compile_error(CompileErrorKind::MissingTypeAnnotation)) Err(self.compile_error(CompileErrorKind::MissingTypeAnnotation))
} }
}).collect::<CompileResult<Vec<types::BasicTypeEnum>>>()?; })
.collect::<CompileResult<Vec<types::BasicTypeEnum>>>()?;
let return_type = if let Some(returns) = returns { let return_type = if let Some(returns) = returns {
self.set_source_location(returns.location); self.set_source_location(returns.location);
if let ast::ExpressionType::Identifier { name } = &returns.node { if let ast::ExpressionType::Identifier { name } = &returns.node {
if name == "None" { None } else { Some(self.get_basic_type(name)?) } if name == "None" {
None
} else { } else {
return Err(self.compile_error(CompileErrorKind::Unsupported("type annotation must be an identifier"))) Some(self.get_basic_type(name)?)
}
} else {
return Err(self.compile_error(CompileErrorKind::Unsupported(
"type annotation must be an identifier",
)));
} }
} else { } else {
None None
@ -186,7 +202,7 @@ impl<'ctx> CodeGen<'ctx> {
let fn_type = match return_type { let fn_type = match return_type {
Some(ty) => ty.fn_type(&args_type, false), Some(ty) => ty.fn_type(&args_type, false),
None => self.context.void_type().fn_type(&args_type, false) None => self.context.void_type().fn_type(&args_type, false),
}; };
let function = self.module.add_function(name, fn_type, None); let function = self.module.add_function(name, fn_type, None);
@ -207,57 +223,74 @@ impl<'ctx> CodeGen<'ctx> {
fn compile_expression( fn compile_expression(
&mut self, &mut self,
expression: &ast::Expression expression: &ast::Expression,
) -> CompileResult<values::BasicValueEnum<'ctx>> { ) -> CompileResult<values::BasicValueEnum<'ctx>> {
self.set_source_location(expression.location); self.set_source_location(expression.location);
match &expression.node { match &expression.node {
ast::ExpressionType::True => Ok(self.context.bool_type().const_int(1, false).into()), ast::ExpressionType::True => Ok(self.context.bool_type().const_int(1, false).into()),
ast::ExpressionType::False => Ok(self.context.bool_type().const_int(0, false).into()), ast::ExpressionType::False => Ok(self.context.bool_type().const_int(0, false).into()),
ast::ExpressionType::Number { value: ast::Number::Integer { value } } => { ast::ExpressionType::Number {
value: ast::Number::Integer { value },
} => {
let mut bits = value.bits(); let mut bits = value.bits();
if value.sign() == num_bigint::Sign::Minus { if value.sign() == num_bigint::Sign::Minus {
bits += 1; bits += 1;
} }
match bits { match bits {
0..=32 => Ok(self.context.i32_type().const_int(value.to_i32().unwrap() as _, true).into()), 0..=32 => Ok(self
33..=64 => Ok(self.context.i64_type().const_int(value.to_i64().unwrap() as _, true).into()), .context
_ => Err(self.compile_error(CompileErrorKind::Unsupported("integers larger than 64 bits"))) .i32_type()
.const_int(value.to_i32().unwrap() as _, true)
.into()),
33..=64 => Ok(self
.context
.i64_type()
.const_int(value.to_i64().unwrap() as _, true)
.into()),
_ => Err(self.compile_error(CompileErrorKind::Unsupported(
"integers larger than 64 bits",
))),
} }
}, }
ast::ExpressionType::Number { value: ast::Number::Float { value } } => { ast::ExpressionType::Number {
Ok(self.context.f64_type().const_float(*value).into()) value: ast::Number::Float { value },
}, } => Ok(self.context.f64_type().const_float(*value).into()),
ast::ExpressionType::Identifier { name } => { ast::ExpressionType::Identifier { name } => match self.namespace.get(name) {
match self.namespace.get(name) {
Some(value) => Ok(self.builder.build_load(*value, name).into()), Some(value) => Ok(self.builder.build_load(*value, name).into()),
None => Err(self.compile_error(CompileErrorKind::UnboundIdentifier)) None => Err(self.compile_error(CompileErrorKind::UnboundIdentifier)),
}
}, },
ast::ExpressionType::Unop { op, a } => { ast::ExpressionType::Unop { op, a } => {
let a = self.compile_expression(&a)?; let a = self.compile_expression(&a)?;
match (op, a) { match (op, a) {
(ast::UnaryOperator::Pos, values::BasicValueEnum::IntValue(a)) (ast::UnaryOperator::Pos, values::BasicValueEnum::IntValue(a)) => Ok(a.into()),
=> Ok(a.into()), (ast::UnaryOperator::Pos, values::BasicValueEnum::FloatValue(a)) => {
(ast::UnaryOperator::Pos, values::BasicValueEnum::FloatValue(a)) Ok(a.into())
=> Ok(a.into()), }
(ast::UnaryOperator::Neg, values::BasicValueEnum::IntValue(a)) (ast::UnaryOperator::Neg, values::BasicValueEnum::IntValue(a)) => {
=> Ok(self.builder.build_int_neg(a, "tmpneg").into()), Ok(self.builder.build_int_neg(a, "tmpneg").into())
(ast::UnaryOperator::Neg, values::BasicValueEnum::FloatValue(a)) }
=> Ok(self.builder.build_float_neg(a, "tmpneg").into()), (ast::UnaryOperator::Neg, values::BasicValueEnum::FloatValue(a)) => {
(ast::UnaryOperator::Inv, values::BasicValueEnum::IntValue(a)) Ok(self.builder.build_float_neg(a, "tmpneg").into())
=> Ok(self.builder.build_not(a, "tmpnot").into()), }
(ast::UnaryOperator::Inv, values::BasicValueEnum::IntValue(a)) => {
Ok(self.builder.build_not(a, "tmpnot").into())
}
(ast::UnaryOperator::Not, values::BasicValueEnum::IntValue(a)) => { (ast::UnaryOperator::Not, values::BasicValueEnum::IntValue(a)) => {
// boolean "not" // boolean "not"
if a.get_type().get_bit_width() != 1 { if a.get_type().get_bit_width() != 1 {
Err(self.compile_error(CompileErrorKind::Unsupported("unimplemented unary operation"))) Err(self.compile_error(CompileErrorKind::Unsupported(
"unimplemented unary operation",
)))
} else { } else {
Ok(self.builder.build_not(a, "tmpnot").into()) Ok(self.builder.build_not(a, "tmpnot").into())
} }
},
_ => Err(self.compile_error(CompileErrorKind::Unsupported("unimplemented unary operation"))),
} }
}, _ => Err(self.compile_error(CompileErrorKind::Unsupported(
"unimplemented unary operation",
))),
}
}
ast::ExpressionType::Binop { a, op, b } => { ast::ExpressionType::Binop { a, op, b } => {
let a = self.compile_expression(&a)?; let a = self.compile_expression(&a)?;
let b = self.compile_expression(&b)?; let b = self.compile_expression(&b)?;
@ -266,27 +299,53 @@ impl<'ctx> CodeGen<'ctx> {
} }
use ast::Operator::*; use ast::Operator::*;
match (op, a, b) { match (op, a, b) {
(Add, values::BasicValueEnum::IntValue(a), values::BasicValueEnum::IntValue(b)) (
=> Ok(self.builder.build_int_add(a, b, "tmpadd").into()), Add,
(Sub, values::BasicValueEnum::IntValue(a), values::BasicValueEnum::IntValue(b)) values::BasicValueEnum::IntValue(a),
=> Ok(self.builder.build_int_sub(a, b, "tmpsub").into()), values::BasicValueEnum::IntValue(b),
(Mult, values::BasicValueEnum::IntValue(a), values::BasicValueEnum::IntValue(b)) ) => Ok(self.builder.build_int_add(a, b, "tmpadd").into()),
=> Ok(self.builder.build_int_mul(a, b, "tmpmul").into()), (
Sub,
values::BasicValueEnum::IntValue(a),
values::BasicValueEnum::IntValue(b),
) => Ok(self.builder.build_int_sub(a, b, "tmpsub").into()),
(
Mult,
values::BasicValueEnum::IntValue(a),
values::BasicValueEnum::IntValue(b),
) => Ok(self.builder.build_int_mul(a, b, "tmpmul").into()),
(Add, values::BasicValueEnum::FloatValue(a), values::BasicValueEnum::FloatValue(b)) (
=> Ok(self.builder.build_float_add(a, b, "tmpadd").into()), Add,
(Sub, values::BasicValueEnum::FloatValue(a), values::BasicValueEnum::FloatValue(b)) values::BasicValueEnum::FloatValue(a),
=> Ok(self.builder.build_float_sub(a, b, "tmpsub").into()), values::BasicValueEnum::FloatValue(b),
(Mult, values::BasicValueEnum::FloatValue(a), values::BasicValueEnum::FloatValue(b)) ) => Ok(self.builder.build_float_add(a, b, "tmpadd").into()),
=> Ok(self.builder.build_float_mul(a, b, "tmpmul").into()), (
Sub,
values::BasicValueEnum::FloatValue(a),
values::BasicValueEnum::FloatValue(b),
) => Ok(self.builder.build_float_sub(a, b, "tmpsub").into()),
(
Mult,
values::BasicValueEnum::FloatValue(a),
values::BasicValueEnum::FloatValue(b),
) => Ok(self.builder.build_float_mul(a, b, "tmpmul").into()),
(Div, values::BasicValueEnum::FloatValue(a), values::BasicValueEnum::FloatValue(b)) (
=> Ok(self.builder.build_float_div(a, b, "tmpdiv").into()), Div,
(FloorDiv, values::BasicValueEnum::IntValue(a), values::BasicValueEnum::IntValue(b)) values::BasicValueEnum::FloatValue(a),
=> Ok(self.builder.build_int_signed_div(a, b, "tmpdiv").into()), values::BasicValueEnum::FloatValue(b),
_ => Err(self.compile_error(CompileErrorKind::Unsupported("unimplemented binary operation"))), ) => Ok(self.builder.build_float_div(a, b, "tmpdiv").into()),
(
FloorDiv,
values::BasicValueEnum::IntValue(a),
values::BasicValueEnum::IntValue(b),
) => Ok(self.builder.build_int_signed_div(a, b, "tmpdiv").into()),
_ => Err(self.compile_error(CompileErrorKind::Unsupported(
"unimplemented binary operation",
))),
}
} }
},
ast::ExpressionType::Compare { vals, ops } => { ast::ExpressionType::Compare { vals, ops } => {
let mut vals = vals.iter(); let mut vals = vals.iter();
let mut ops = ops.iter(); let mut ops = ops.iter();
@ -299,42 +358,92 @@ impl<'ctx> CodeGen<'ctx> {
if a.get_type() != b.get_type() { if a.get_type() != b.get_type() {
return Err(self.compile_error(CompileErrorKind::IncompatibleTypes)); return Err(self.compile_error(CompileErrorKind::IncompatibleTypes));
} }
let this_result = match (a, b) { let this_result =
(values::BasicValueEnum::IntValue(a), values::BasicValueEnum::IntValue(b)) => { match (a, b) {
(
values::BasicValueEnum::IntValue(a),
values::BasicValueEnum::IntValue(b),
) => {
match op { match op {
ast::Comparison::Equal ast::Comparison::Equal => self.builder.build_int_compare(
=> self.builder.build_int_compare(IntPredicate::EQ, a, b, "tmpeq"), IntPredicate::EQ,
ast::Comparison::NotEqual a,
=> self.builder.build_int_compare(IntPredicate::NE, a, b, "tmpne"), b,
ast::Comparison::Less "tmpeq",
=> self.builder.build_int_compare(IntPredicate::SLT, a, b, "tmpslt"), ),
ast::Comparison::LessOrEqual ast::Comparison::NotEqual => self
=> self.builder.build_int_compare(IntPredicate::SLE, a, b, "tmpsle"), .builder
ast::Comparison::Greater .build_int_compare(IntPredicate::NE, a, b, "tmpne"),
=> self.builder.build_int_compare(IntPredicate::SGT, a, b, "tmpsgt"), ast::Comparison::Less => self.builder.build_int_compare(
ast::Comparison::GreaterOrEqual IntPredicate::SLT,
=> self.builder.build_int_compare(IntPredicate::SGE, a, b, "tmpsge"), a,
_ => return Err(self.compile_error(CompileErrorKind::Unsupported("special comparison"))), b,
"tmpslt",
),
ast::Comparison::LessOrEqual => self
.builder
.build_int_compare(IntPredicate::SLE, a, b, "tmpsle"),
ast::Comparison::Greater => self.builder.build_int_compare(
IntPredicate::SGT,
a,
b,
"tmpsgt",
),
ast::Comparison::GreaterOrEqual => self
.builder
.build_int_compare(IntPredicate::SGE, a, b, "tmpsge"),
_ => {
return Err(self.compile_error(
CompileErrorKind::Unsupported("special comparison"),
))
}
}
}
(
values::BasicValueEnum::FloatValue(a),
values::BasicValueEnum::FloatValue(b),
) => match op {
ast::Comparison::Equal => self.builder.build_float_compare(
FloatPredicate::OEQ,
a,
b,
"tmpoeq",
),
ast::Comparison::NotEqual => self.builder.build_float_compare(
FloatPredicate::UNE,
a,
b,
"tmpune",
),
ast::Comparison::Less => self.builder.build_float_compare(
FloatPredicate::OLT,
a,
b,
"tmpolt",
),
ast::Comparison::LessOrEqual => self
.builder
.build_float_compare(FloatPredicate::OLE, a, b, "tmpole"),
ast::Comparison::Greater => self.builder.build_float_compare(
FloatPredicate::OGT,
a,
b,
"tmpogt",
),
ast::Comparison::GreaterOrEqual => self
.builder
.build_float_compare(FloatPredicate::OGE, a, b, "tmpoge"),
_ => {
return Err(self.compile_error(
CompileErrorKind::Unsupported("special comparison"),
))
} }
}, },
(values::BasicValueEnum::FloatValue(a), values::BasicValueEnum::FloatValue(b)) => { _ => {
match op { return Err(self.compile_error(CompileErrorKind::Unsupported(
ast::Comparison::Equal "comparison of non-numerical types",
=> self.builder.build_float_compare(FloatPredicate::OEQ, a, b, "tmpoeq"), )))
ast::Comparison::NotEqual
=> self.builder.build_float_compare(FloatPredicate::UNE, a, b, "tmpune"),
ast::Comparison::Less
=> self.builder.build_float_compare(FloatPredicate::OLT, a, b, "tmpolt"),
ast::Comparison::LessOrEqual
=> self.builder.build_float_compare(FloatPredicate::OLE, a, b, "tmpole"),
ast::Comparison::Greater
=> self.builder.build_float_compare(FloatPredicate::OGT, a, b, "tmpogt"),
ast::Comparison::GreaterOrEqual
=> self.builder.build_float_compare(FloatPredicate::OGE, a, b, "tmpoge"),
_ => return Err(self.compile_error(CompileErrorKind::Unsupported("special comparison"))),
} }
},
_ => return Err(self.compile_error(CompileErrorKind::Unsupported("comparison of non-numerical types"))),
}; };
match result { match result {
Some(last) => { Some(last) => {
@ -346,15 +455,23 @@ impl<'ctx> CodeGen<'ctx> {
} }
a = b; a = b;
} else { } else {
return Ok(result.unwrap().into()) return Ok(result.unwrap().into());
} }
} }
}, }
ast::ExpressionType::Call { function, args, keywords } => { ast::ExpressionType::Call {
function,
args,
keywords,
} => {
if !keywords.is_empty() { if !keywords.is_empty() {
return Err(self.compile_error(CompileErrorKind::Unsupported("keyword arguments"))) return Err(
self.compile_error(CompileErrorKind::Unsupported("keyword arguments"))
);
} }
let args = args.iter().map(|val| self.compile_expression(val)) let args = args
.iter()
.map(|val| self.compile_expression(val))
.collect::<CompileResult<Vec<values::BasicValueEnum>>>()?; .collect::<CompileResult<Vec<values::BasicValueEnum>>>()?;
self.set_source_location(expression.location); self.set_source_location(expression.location);
if let ast::ExpressionType::Identifier { name } = &function.node { if let ast::ExpressionType::Identifier { name } = &function.node {
@ -362,67 +479,99 @@ impl<'ctx> CodeGen<'ctx> {
("int32", values::BasicValueEnum::IntValue(a)) => { ("int32", values::BasicValueEnum::IntValue(a)) => {
let nbits = a.get_type().get_bit_width(); let nbits = a.get_type().get_bit_width();
if nbits < 32 { if nbits < 32 {
Ok(self.builder.build_int_s_extend(a, self.context.i32_type(), "tmpsext").into()) Ok(self
.builder
.build_int_s_extend(a, self.context.i32_type(), "tmpsext")
.into())
} else if nbits > 32 { } else if nbits > 32 {
Ok(self.builder.build_int_truncate(a, self.context.i32_type(), "tmptrunc").into()) Ok(self
.builder
.build_int_truncate(a, self.context.i32_type(), "tmptrunc")
.into())
} else { } else {
Ok(a.into()) Ok(a.into())
} }
}, }
("int64", values::BasicValueEnum::IntValue(a)) => { ("int64", values::BasicValueEnum::IntValue(a)) => {
let nbits = a.get_type().get_bit_width(); let nbits = a.get_type().get_bit_width();
if nbits < 64 { if nbits < 64 {
Ok(self.builder.build_int_s_extend(a, self.context.i64_type(), "tmpsext").into()) Ok(self
.builder
.build_int_s_extend(a, self.context.i64_type(), "tmpsext")
.into())
} else { } else {
Ok(a.into()) Ok(a.into())
} }
}, }
("int32", values::BasicValueEnum::FloatValue(a)) => { ("int32", values::BasicValueEnum::FloatValue(a)) => Ok(self
Ok(self.builder.build_float_to_signed_int(a, self.context.i32_type(), "tmpfptosi").into()) .builder
}, .build_float_to_signed_int(a, self.context.i32_type(), "tmpfptosi")
("int64", values::BasicValueEnum::FloatValue(a)) => { .into()),
Ok(self.builder.build_float_to_signed_int(a, self.context.i64_type(), "tmpfptosi").into()) ("int64", values::BasicValueEnum::FloatValue(a)) => Ok(self
}, .builder
("float32", values::BasicValueEnum::IntValue(a)) => { .build_float_to_signed_int(a, self.context.i64_type(), "tmpfptosi")
Ok(self.builder.build_signed_int_to_float(a, self.context.f32_type(), "tmpsitofp").into()) .into()),
}, ("float32", values::BasicValueEnum::IntValue(a)) => Ok(self
("float64", values::BasicValueEnum::IntValue(a)) => { .builder
Ok(self.builder.build_signed_int_to_float(a, self.context.f64_type(), "tmpsitofp").into()) .build_signed_int_to_float(a, self.context.f32_type(), "tmpsitofp")
}, .into()),
("float64", values::BasicValueEnum::IntValue(a)) => Ok(self
.builder
.build_signed_int_to_float(a, self.context.f64_type(), "tmpsitofp")
.into()),
("float32", values::BasicValueEnum::FloatValue(a)) => { ("float32", values::BasicValueEnum::FloatValue(a)) => {
if a.get_type() == self.context.f64_type() { if a.get_type() == self.context.f64_type() {
Ok(self.builder.build_float_trunc(a, self.context.f32_type(), "tmptrunc").into()) Ok(self
.builder
.build_float_trunc(a, self.context.f32_type(), "tmptrunc")
.into())
} else { } else {
Ok(a.into()) Ok(a.into())
} }
}, }
("float64", values::BasicValueEnum::FloatValue(a)) => { ("float64", values::BasicValueEnum::FloatValue(a)) => {
if a.get_type() == self.context.f32_type() { if a.get_type() == self.context.f32_type() {
Ok(self.builder.build_float_ext(a, self.context.f64_type(), "tmpext").into()) Ok(self
.builder
.build_float_ext(a, self.context.f64_type(), "tmpext")
.into())
} else { } else {
Ok(a.into()) Ok(a.into())
} }
}, }
("output", values::BasicValueEnum::IntValue(a)) => { ("output", values::BasicValueEnum::IntValue(a)) => {
let fn_value = self.module.get_function("output").unwrap(); let fn_value = self.module.get_function("output").unwrap();
Ok(self.builder.build_call(fn_value, &[a.into()], "call") Ok(self
.try_as_basic_value().left().unwrap()) .builder
}, .build_call(fn_value, &[a.into()], "call")
_ => Err(self.compile_error(CompileErrorKind::Unsupported("unrecognized call"))) .try_as_basic_value()
.left()
.unwrap())
}
_ => {
Err(self
.compile_error(CompileErrorKind::Unsupported("unrecognized call")))
}
} }
} else { } else {
return Err(self.compile_error(CompileErrorKind::Unsupported("function must be an identifier"))) return Err(self.compile_error(CompileErrorKind::Unsupported(
"function must be an identifier",
)));
}
}
_ => {
return Err(
self.compile_error(CompileErrorKind::Unsupported("unimplemented expression"))
)
} }
},
_ => return Err(self.compile_error(CompileErrorKind::Unsupported("unimplemented expression"))),
} }
} }
fn compile_statement( fn compile_statement(
&mut self, &mut self,
statement: &ast::Statement, statement: &ast::Statement,
return_type: Option<types::BasicTypeEnum> return_type: Option<types::BasicTypeEnum>,
) -> CompileResult<()> { ) -> CompileResult<()> {
self.set_source_location(statement.location); self.set_source_location(statement.location);
@ -434,29 +583,43 @@ impl<'ctx> CodeGen<'ctx> {
self.set_source_location(target.location); self.set_source_location(target.location);
if let ast::ExpressionType::Identifier { name } = &target.node { if let ast::ExpressionType::Identifier { name } = &target.node {
let builder = &self.builder; let builder = &self.builder;
let target = self.namespace.entry(name.clone()).or_insert_with( let target = self
|| builder.build_alloca(value.get_type(), name)); .namespace
if target.get_type() != value.get_type().ptr_type(inkwell::AddressSpace::Generic) { .entry(name.clone())
.or_insert_with(|| builder.build_alloca(value.get_type(), name));
if target.get_type()
!= value.get_type().ptr_type(inkwell::AddressSpace::Generic)
{
return Err(self.compile_error(CompileErrorKind::IncompatibleTypes)); return Err(self.compile_error(CompileErrorKind::IncompatibleTypes));
} }
builder.build_store(*target, value); builder.build_store(*target, value);
} else { } else {
return Err(self.compile_error(CompileErrorKind::Unsupported("assignment target must be an identifier"))) return Err(self.compile_error(CompileErrorKind::Unsupported(
"assignment target must be an identifier",
)));
} }
} }
}, }
Expression { expression } => { self.compile_expression(expression)?; }, Expression { expression } => {
self.compile_expression(expression)?;
}
If { test, body, orelse } => { If { test, body, orelse } => {
let test = self.compile_expression(test)?; let test = self.compile_expression(test)?;
if test.get_type() != self.context.bool_type().into() { if test.get_type() != self.context.bool_type().into() {
return Err(self.compile_error(CompileErrorKind::IncompatibleTypes)); return Err(self.compile_error(CompileErrorKind::IncompatibleTypes));
} }
let parent = self.builder.get_insert_block().unwrap().get_parent().unwrap(); let parent = self
.builder
.get_insert_block()
.unwrap()
.get_parent()
.unwrap();
let then_bb = self.context.append_basic_block(parent, "then"); let then_bb = self.context.append_basic_block(parent, "then");
let else_bb = self.context.append_basic_block(parent, "else"); let else_bb = self.context.append_basic_block(parent, "else");
let cont_bb = self.context.append_basic_block(parent, "ifcont"); let cont_bb = self.context.append_basic_block(parent, "ifcont");
self.builder.build_conditional_branch(test.into_int_value(), then_bb, else_bb); self.builder
.build_conditional_branch(test.into_int_value(), then_bb, else_bb);
self.builder.position_at_end(then_bb); self.builder.position_at_end(then_bb);
self.compile_suite(body, return_type)?; self.compile_suite(body, return_type)?;
@ -468,9 +631,14 @@ impl<'ctx> CodeGen<'ctx> {
} }
self.builder.build_unconditional_branch(cont_bb); self.builder.build_unconditional_branch(cont_bb);
self.builder.position_at_end(cont_bb); self.builder.position_at_end(cont_bb);
}, }
While { test, body, orelse } => { While { test, body, orelse } => {
let parent = self.builder.get_insert_block().unwrap().get_parent().unwrap(); let parent = self
.builder
.get_insert_block()
.unwrap()
.get_parent()
.unwrap();
let test_bb = self.context.append_basic_block(parent, "test"); let test_bb = self.context.append_basic_block(parent, "test");
self.builder.build_unconditional_branch(test_bb); self.builder.build_unconditional_branch(test_bb);
self.builder.position_at_end(test_bb); self.builder.position_at_end(test_bb);
@ -482,7 +650,8 @@ impl<'ctx> CodeGen<'ctx> {
let then_bb = self.context.append_basic_block(parent, "then"); let then_bb = self.context.append_basic_block(parent, "then");
let else_bb = self.context.append_basic_block(parent, "else"); let else_bb = self.context.append_basic_block(parent, "else");
let cont_bb = self.context.append_basic_block(parent, "ifcont"); let cont_bb = self.context.append_basic_block(parent, "ifcont");
self.builder.build_conditional_branch(test.into_int_value(), then_bb, else_bb); self.builder
.build_conditional_branch(test.into_int_value(), then_bb, else_bb);
self.break_bb = Some(cont_bb); self.break_bb = Some(cont_bb);
@ -498,11 +667,16 @@ impl<'ctx> CodeGen<'ctx> {
self.builder.position_at_end(cont_bb); self.builder.position_at_end(cont_bb);
self.break_bb = None; self.break_bb = None;
}, }
Break => { Break => {
if let Some(bb) = self.break_bb { if let Some(bb) = self.break_bb {
self.builder.build_unconditional_branch(bb); self.builder.build_unconditional_branch(bb);
let parent = self.builder.get_insert_block().unwrap().get_parent().unwrap(); let parent = self
.builder
.get_insert_block()
.unwrap()
.get_parent()
.unwrap();
let unreachable_bb = self.context.append_basic_block(parent, "unreachable"); let unreachable_bb = self.context.append_basic_block(parent, "unreachable");
self.builder.position_at_end(unreachable_bb); self.builder.position_at_end(unreachable_bb);
} else { } else {
@ -519,13 +693,13 @@ impl<'ctx> CodeGen<'ctx> {
} else { } else {
return Err(self.compile_error(CompileErrorKind::IncompatibleTypes)); return Err(self.compile_error(CompileErrorKind::IncompatibleTypes));
} }
}, }
Return { value: None } => { Return { value: None } => {
if !return_type.is_none() { if !return_type.is_none() {
return Err(self.compile_error(CompileErrorKind::IncompatibleTypes)); return Err(self.compile_error(CompileErrorKind::IncompatibleTypes));
} }
self.builder.build_return(None); self.builder.build_return(None);
}, }
Pass => (), Pass => (),
_ => return Err(self.compile_error(CompileErrorKind::Unsupported("special statement"))), _ => return Err(self.compile_error(CompileErrorKind::Unsupported("special statement"))),
} }
@ -535,7 +709,7 @@ impl<'ctx> CodeGen<'ctx> {
fn compile_suite( fn compile_suite(
&mut self, &mut self,
suite: &ast::Suite, suite: &ast::Suite,
return_type: Option<types::BasicTypeEnum> return_type: Option<types::BasicTypeEnum>,
) -> CompileResult<()> { ) -> CompileResult<()> {
for statement in suite.iter() { for statement in suite.iter() {
self.compile_statement(statement, return_type)?; self.compile_statement(statement, return_type)?;
@ -552,12 +726,16 @@ impl<'ctx> CodeGen<'ctx> {
body, body,
decorator_list, decorator_list,
returns, returns,
} = &statement.node { } = &statement.node
let function = self.compile_function_def(name, args, body, decorator_list, returns, *is_async)?; {
let function =
self.compile_function_def(name, args, body, decorator_list, returns, *is_async)?;
self.pass_manager.run_on(&function); self.pass_manager.run_on(&function);
Ok(()) Ok(())
} else { } else {
Err(self.compile_error(CompileErrorKind::Internal("top-level is not a function definition"))) Err(self.compile_error(CompileErrorKind::Internal(
"top-level is not a function definition",
)))
} }
} }
@ -568,8 +746,8 @@ impl<'ctx> CodeGen<'ctx> {
pub fn output(&self, filename: &str) { pub fn output(&self, filename: &str) {
//let triple = TargetTriple::create("riscv32-none-linux-gnu"); //let triple = TargetTriple::create("riscv32-none-linux-gnu");
let triple = TargetMachine::get_default_triple(); let triple = TargetMachine::get_default_triple();
let target = Target::from_triple(&triple) let target =
.expect("couldn't create target from target triple"); Target::from_triple(&triple).expect("couldn't create target from target triple");
let target_machine = target let target_machine = target
.create_target_machine( .create_target_machine(

View File

@ -56,4 +56,3 @@ pub fn comparison_name(op: &Comparison) -> Option<&'static str> {
_ => None, _ => None,
} }
} }

View File

@ -215,8 +215,7 @@ impl Type {
pub fn get_base<'b: 'a, 'a>(&'a self, ctx: &'b GlobalContext) -> Option<&'b TypeDef> { pub fn get_base<'b: 'a, 'a>(&'a self, ctx: &'b GlobalContext) -> Option<&'b TypeDef> {
match self { match self {
Type::PrimitiveType(id) => Some(ctx.get_primitive(*id)), Type::PrimitiveType(id) => Some(ctx.get_primitive(*id)),
Type::ClassType(id) Type::ClassType(id) | Type::VirtualClassType(id) => Some(&ctx.get_class(*id).base),
| Type::VirtualClassType(id) => Some(&ctx.get_class(*id).base),
Type::ParametricType(id, _) => Some(&ctx.get_parametric(*id).base), Type::ParametricType(id, _) => Some(&ctx.get_parametric(*id).base),
_ => None, _ => None,
} }