Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for externs, pointers (ffi), casts #95

Merged
merged 8 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 34 additions & 34 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ Features:
- floats ✔️
- borrowing ✔️
- structs ✔️
- casts ✔️
- arrays 🏗️
- iterators :x:
- for :x:
Expand Down
23 changes: 9 additions & 14 deletions crates/concrete_ast/src/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::collections::HashMap;
use crate::{
common::{Ident, Span},
statements::Statement,
types::{RefType, TypeSpec},
types::TypeSpec,
};

#[derive(Clone, Debug, Eq, PartialEq)]
Expand All @@ -15,17 +15,18 @@ pub enum Expression {
UnaryOp(UnaryOp, Box<Self>),
BinaryOp(Box<Self>, BinaryOp, Box<Self>),
StructInit(StructInitExpr),
Deref(Box<Self>),
AsRef(Box<Self>, RefType),
Deref(Box<Self>, Span),
AsRef(Box<Self>, bool, Span),
Cast(Box<Self>, TypeSpec, Span),
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ValueExpr {
ConstBool(bool),
ConstChar(char),
ConstInt(u128),
ConstFloat(String),
ConstStr(String),
ConstBool(bool, Span),
ConstChar(char, Span),
ConstInt(u128, Span),
ConstFloat(String, Span),
ConstStr(String, Span),
Path(PathOp),
}

Expand Down Expand Up @@ -124,12 +125,6 @@ pub struct PathOp {
pub span: Span,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct CastOp {
pub value: Expression,
pub r#type: TypeSpec,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct FnCallOp {
pub target: Ident,
Expand Down
3 changes: 3 additions & 0 deletions crates/concrete_ast/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ pub struct FunctionDecl {
pub name: Ident,
pub params: Vec<Param>,
pub ret_type: Option<TypeSpec>,
pub is_extern: bool,
pub is_pub: bool,
pub span: Span,
}

#[derive(Clone, Debug, Eq, PartialEq)]
Expand Down
3 changes: 2 additions & 1 deletion crates/concrete_ast/src/modules.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
common::{DocString, Ident, Span},
constants::ConstantDef,
functions::FunctionDef,
functions::{FunctionDecl, FunctionDef},
imports::ImportStmt,
structs::StructDecl,
types::TypeDecl,
Expand All @@ -20,6 +20,7 @@ pub struct Module {
pub enum ModuleDefItem {
Constant(ConstantDef),
Function(FunctionDef),
FunctionDecl(FunctionDecl),
Struct(StructDecl),
Type(TypeDecl),
Module(Module),
Expand Down
24 changes: 9 additions & 15 deletions crates/concrete_ast/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,41 +1,35 @@
use crate::common::{DocString, Ident, Span};

#[derive(Clone, Debug, Eq, Hash, PartialEq, Copy)]
pub enum RefType {
Borrow,
MutBorrow,
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum TypeQualifier {
Ref, // &
RefMut, // &mut
Ptr, // *const
PtrMut, // *mut
}

#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub enum TypeSpec {
Simple {
name: Ident,
is_ref: Option<RefType>,
qualifiers: Vec<TypeQualifier>,
span: Span,
},
Generic {
name: Ident,
is_ref: Option<RefType>,
qualifiers: Vec<TypeQualifier>,
type_params: Vec<TypeSpec>,
span: Span,
},
Array {
of_type: Box<Self>,
size: u64,
is_ref: Option<RefType>,
qualifiers: Vec<TypeQualifier>,
span: Span,
},
}

impl TypeSpec {
pub fn is_ref(&self) -> Option<RefType> {
match self {
TypeSpec::Simple { is_ref, .. } => *is_ref,
TypeSpec::Generic { is_ref, .. } => *is_ref,
TypeSpec::Array { is_ref, .. } => *is_ref,
}
}

pub fn get_name(&self) -> String {
match self {
TypeSpec::Simple { name, .. } => name.name.clone(),
Expand Down
12 changes: 11 additions & 1 deletion crates/concrete_check/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ pub fn lowering_error_to_report(
LoweringError::UnexpectedType { span, found, expected } => {
let mut labels = vec![
Label::new((path.clone(), span.into()))
.with_message(format!("Unexpected type '{}', expected '{}'", found, expected.kind))
.with_message(format!("Unexpected type '{}', expected '{}'", found.kind, expected.kind))
.with_color(colors.next())
];

Expand Down Expand Up @@ -146,5 +146,15 @@ pub fn lowering_error_to_report(
)
.finish()
},
LoweringError::ExternFnWithBody { span, name } => {
Report::build(ReportKind::Error, path.clone(), span.from)
.with_code("ExternFnWithBody")
.with_label(
Label::new((path, span.into()))
.with_message(format!("extern function {:?} declared with body", name))
.with_color(colors.next()),
)
.finish()
},
}
}
Loading
Loading