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)
{
printf(msg);
printf("%s", msg);
}
/* END builtins */

2
examples/fib.sb

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

13
examples/playground.sb

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

19
src/generator/c.rs

@ -27,6 +27,11 @@ impl Generator for CGenerator {
crate::Builtins::get("builtin.c").expect("Could not locate builtin functions");
code += std::str::from_utf8(raw_builtins.as_ref())
.expect("Unable to interpret builtin functions");
for func in &prog.func {
code += &format!("{};\n", &generate_function_signature(func.clone()));
}
let funcs: String = prog
.func
.into_iter()
@ -63,6 +68,14 @@ pub(super) fn generate_type(t: Either<Variable, Option<Type>>) -> 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
.arguments
.into_iter()
@ -70,11 +83,7 @@ fn generate_function(func: Function) -> String {
.collect::<Vec<String>>()
.join(", ");
let t = generate_type(Either::Right(func.ret_type));
let mut raw = format!("{T} {N}({A})", T = t, N = func.name, A = arguments);
raw += &generate_block(func.body);
raw
format!("{T} {N}({A})", T = t, N = func.name, A = arguments)
}
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 name: String,
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 {
Block(Vec<Statement>),
Declare(Variable, Option<Expression>),
@ -74,7 +74,7 @@ pub enum Statement {
Exp(Expression),
}
#[derive(Debug, Eq, PartialEq)]
#[derive(Debug, Eq, PartialEq, Clone)]
pub enum Expression {
Int(u32),
Str(String),
@ -111,7 +111,7 @@ impl TryFrom<Token> for Expression {
}
}
#[derive(Debug, Eq, PartialEq)]
#[derive(Debug, Eq, PartialEq, Clone)]
pub enum BinOp {
Addition,
Subtraction,

Loading…
Cancel
Save