Browse Source

Generate function prototypes

github-actions
Garrit Franke 3 years ago
parent
commit
949b4dc449
  1. BIN
      a.out
  2. 2
      builtin/builtin.c
  3. 2
      examples/fib.sb
  4. 13
      examples/playground.sb
  5. 19
      src/generator/c.rs
  6. 8
      src/parser/node_type.rs

BIN
a.out

Binary file not shown.

2
builtin/builtin.c

@ -4,7 +4,7 @@
void _printf(char *msg) void _printf(char *msg)
{ {
printf(msg); printf("%s", msg);
} }
/* END builtins */ /* END builtins */

2
examples/fib.sb

@ -1,5 +1,5 @@
fn main() { fn main() {
let num = 10 let num: int = 10
println(fib(num)) println(fib(num))
} }

13
examples/playground.sb

@ -1,11 +1,4 @@
fn main(n: int) { fn main() {
_printf("Hello World!\n") let x: string = "Hello World\n"
_printf(x)
if n > 2 {
_printf("Larger than 2\n")
} else if n == 2 {
_printf("Is 2\n")
} else {
_printf("Less than 2\n")
}
} }

19
src/generator/c.rs

@ -27,6 +27,11 @@ impl Generator for CGenerator {
crate::Builtins::get("builtin.c").expect("Could not locate builtin functions"); crate::Builtins::get("builtin.c").expect("Could not locate builtin functions");
code += std::str::from_utf8(raw_builtins.as_ref()) code += std::str::from_utf8(raw_builtins.as_ref())
.expect("Unable to interpret builtin functions"); .expect("Unable to interpret builtin functions");
for func in &prog.func {
code += &format!("{};\n", &generate_function_signature(func.clone()));
}
let funcs: String = prog let funcs: String = prog
.func .func
.into_iter() .into_iter()
@ -63,6 +68,14 @@ pub(super) fn generate_type(t: Either<Variable, Option<Type>>) -> String {
} }
fn generate_function(func: Function) -> String { fn generate_function(func: Function) -> String {
let mut buf = String::new();
buf += &format!("{} ", &generate_function_signature(func.clone()));
buf += &generate_block(func.body);
buf
}
fn generate_function_signature(func: Function) -> String {
let arguments: String = func let arguments: String = func
.arguments .arguments
.into_iter() .into_iter()
@ -70,11 +83,7 @@ fn generate_function(func: Function) -> String {
.collect::<Vec<String>>() .collect::<Vec<String>>()
.join(", "); .join(", ");
let t = generate_type(Either::Right(func.ret_type)); let t = generate_type(Either::Right(func.ret_type));
let mut raw = format!("{T} {N}({A})", T = t, N = func.name, A = arguments); format!("{T} {N}({A})", T = t, N = func.name, A = arguments)
raw += &generate_block(func.body);
raw
} }
fn generate_block(block: Statement) -> String { fn generate_block(block: Statement) -> String {

8
src/parser/node_type.rs

@ -29,7 +29,7 @@ impl Program {
} }
} }
#[derive(Debug)] #[derive(Debug, Clone)]
pub struct Function { pub struct Function {
pub name: String, pub name: String,
pub arguments: Vec<Variable>, pub arguments: Vec<Variable>,
@ -63,7 +63,7 @@ impl TryFrom<String> for Type {
} }
} }
#[derive(Debug, Eq, PartialEq)] #[derive(Debug, Eq, PartialEq, Clone)]
pub enum Statement { pub enum Statement {
Block(Vec<Statement>), Block(Vec<Statement>),
Declare(Variable, Option<Expression>), Declare(Variable, Option<Expression>),
@ -74,7 +74,7 @@ pub enum Statement {
Exp(Expression), Exp(Expression),
} }
#[derive(Debug, Eq, PartialEq)] #[derive(Debug, Eq, PartialEq, Clone)]
pub enum Expression { pub enum Expression {
Int(u32), Int(u32),
Str(String), Str(String),
@ -111,7 +111,7 @@ impl TryFrom<Token> for Expression {
} }
} }
#[derive(Debug, Eq, PartialEq)] #[derive(Debug, Eq, PartialEq, Clone)]
pub enum BinOp { pub enum BinOp {
Addition, Addition,
Subtraction, Subtraction,

Loading…
Cancel
Save