Skip to content

Commit

Permalink
Got the first implementations working
Browse files Browse the repository at this point in the history
  • Loading branch information
JCBurnside committed Sep 24, 2024
1 parent 82e8ebc commit 51dc21a
Show file tree
Hide file tree
Showing 13 changed files with 1,163 additions and 335 deletions.
5 changes: 3 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [

{
"name": "(Windows) Launch",
"type": "cppvsdbg",
Expand Down Expand Up @@ -33,10 +34,10 @@
// },

"program": "${workspaceFolder}/target/debug/cli",
"args":["--llvm", "-o", "./test_output.llvm", "./sample.fb"],
"args":["--llvm", "-o", "./test_output.llvm", "./other_sample.fb"],
"cwd": "${workspaceFolder}",
"env": {"RUST_BACKTRACE": "1"},
"console": "externalTerminal",
"console": "integratedTerminal",
"preLaunchTask": "build"
}
]
Expand Down
4 changes: 2 additions & 2 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,13 @@ fn main() {
}
}
Ok(ast) => {
if args.run {
if dbg!(args.run) {
let mut jit = llvm_codegen::create_jit_runtime();
jit.add_declarations(ast.declarations);
unsafe {
jit.run_function::<unsafe extern "C" fn()>("main", ());
}
} else if args.output_llvm {
} else if dbg!(args.output_llvm) {
llvm_codegen::compile_file(ast, args.file, args.out_file, fwd_decl)
}
}
Expand Down
193 changes: 129 additions & 64 deletions compiler/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,80 +97,103 @@ impl ModuleDeclaration {
s.ident.clone(),
ResolvedType::User {
name: s.ident.clone(),
generics: s.generics
.as_ref()
.map(|generics|
generics.decls.iter().map(|(loc,name)|
ResolvedType::Generic { name:name.clone(), loc:*loc }
).collect())
.unwrap_or_default(),
generics: s
.generics
.as_ref()
.map(|generics| {
generics
.decls
.iter()
.map(|(loc, name)| ResolvedType::Generic {
name: name.clone(),
loc: *loc,
})
.collect()
})
.unwrap_or_default(),
loc: s.loc,
}
)].into(),
},
)]
.into(),
TopLevelDeclaration::TypeDefinition(TypeDefinition::Enum(e)) => {
let base_ty = ResolvedType::User {
name: e.ident.clone(),
generics: e.generics
.as_ref()
.map(|generics|
generics.decls.iter().map(|(loc,name)|
ResolvedType::Generic { name:name.clone(), loc:*loc }
).collect())
.unwrap_or_default(),
generics: e
.generics
.as_ref()
.map(|generics| {
generics
.decls
.iter()
.map(|(loc, name)| ResolvedType::Generic {
name: name.clone(),
loc: *loc,
})
.collect()
})
.unwrap_or_default(),
loc: e.loc,
};
let mut out = vec![
(e.ident.clone(),base_ty.clone())
];
let mut out = vec![(e.ident.clone(), base_ty.clone())];

for variant in &e.values {
let ident = variant.get_ident();
let new_ident = format!("{}::{}", &e.ident, ident);
let generics = e
.generics
.as_ref()
.map(|generics| {
generics
.decls
.iter()
.map(|(loc, name)| ResolvedType::Generic {
name: name.clone(),
loc: *loc,
})
.collect()
})
.unwrap_or_default();
match variant {
EnumVariant::Unit { ident, loc } => (),
EnumVariant::Tuple { ident, ty, loc, } =>
out.push(
(
format!("{}::{}",&e.ident,ident),
ResolvedType::Dependent {
base: base_ty.clone().into(),
ident: ident.clone(),
actual : ty.clone().into(),
generics:e.generics
.as_ref()
.map(|generics|
generics.decls.iter().map(|(loc,name)|
ResolvedType::Generic { name:name.clone(), loc:*loc }
).collect())
.unwrap_or_default(),
loc: *loc
}
)
),
EnumVariant::Struct { ident, fields, loc, .. } => {
let new_ident = format!("{}::{}",&e.ident,ident);
let generics : Vec<_> = e.generics
.as_ref()
.map(|generics|
generics.decls.iter().map(|(loc,name)|
ResolvedType::Generic { name:name.clone(), loc:*loc }
).collect())
.unwrap_or_default();
out.push(
(
new_ident.clone(),
ResolvedType::Dependent {
base: base_ty.clone().into(),
ident: ident.clone(),
actual : ResolvedType::User {
name: new_ident,
generics: generics.clone(),
loc: *loc
}.into(),
generics,
loc: *loc
EnumVariant::Unit { ident:_, loc } => out.push((
new_ident.clone(),
ResolvedType::Dependent {
base: base_ty.clone().into(),
ident: new_ident.clone(),
actual: ResolvedType::Void.into(),
generics,
loc: *loc,
},
)),
EnumVariant::Tuple { ident:_, ty, loc } => out.push((
new_ident.clone(),
ResolvedType::Dependent {
base: base_ty.clone().into(),
ident: new_ident,
actual: ty.clone().into(),
generics,
loc: *loc,
},
)),
EnumVariant::Struct {
ident:_, loc, ..
} => {

out.push((
new_ident.clone(),
ResolvedType::Dependent {
base: base_ty.clone().into(),
ident: new_ident.clone(),
actual: ResolvedType::User {
name: new_ident,
generics:generics.clone(),
loc: *loc,
}
)
)
},
.into(),
generics,
loc: *loc,
},
))
}
}
}
HashMap::from_iter(out)
Expand Down Expand Up @@ -997,6 +1020,7 @@ pub struct MatchArm {
}
impl MatchArm {
fn replace(&mut self, nice_name: &str, actual: &str) {
self.cond.replace(nice_name,actual);
let names = self.cond.get_idents();
if names.contains(nice_name) {
return;
Expand Down Expand Up @@ -1055,6 +1079,47 @@ impl Pattern {
_ => HashSet::new(),
}
}

fn replace(&mut self, nice_name: &str, actual: &str) {
match self {

Self::Destructure(destructure) => match destructure {
PatternDestructure::Struct { base_ty, .. } => {
if let Some(ty) = base_ty {
if ty == nice_name {
*ty = actual.into();
}
}
},
PatternDestructure::Tuple(pats) => {
for pat in pats {
pat.replace(nice_name,actual)
}
},
PatternDestructure::Unit => (),
},
Self::EnumVariant { ty, variant, pattern, .. } => {
if let Some(ty) = ty {
if ty == nice_name {
*ty = actual.into();
}
} else {
if variant == nice_name {
*variant = actual.into();
}
}
if let Some(pat) = pattern {
pat.replace(nice_name,actual);
}
},

Self::Or(lhs, rhs) => {
lhs.replace(nice_name,actual);
rhs.replace(nice_name,actual);
}
_=>()
}
}
}

#[derive(Debug, PartialEq, Eq)]
Expand Down
Loading

0 comments on commit 51dc21a

Please sign in to comment.