Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
intermediate commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Max Oberaigner committed May 28, 2024
1 parent 7bacb09 commit fe84a38
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 43 deletions.
111 changes: 94 additions & 17 deletions rust/godot-plugin/src/conversion.rs
Original file line number Diff line number Diff line change
@@ -1,67 +1,144 @@
use godot::prelude::*;

use crate::GodotASTNode;
use compiler::Ast;

pub fn to_godot_ast(a: &compiler::ast::Statement) -> Gd<GodotASTNode> {
#[allow(dead_code)]
#[derive(GodotClass)]
#[class(init, base=Node)]
pub struct GodotASTNode {
node_type: i64,
path: Array<i64>,
member_data: VariantArray,
}

pub type ASTArray = Array<Gd<GodotASTNode>>;
pub type PathArray = Array<i64>;

trait PathNavigatable {
fn top() -> Self;
fn enter(&self) -> Self;
fn exit(&self) -> Self;
fn next(&self) -> Self;
}

impl PathNavigatable for PathArray {
fn top() -> Self {
array![0]
}

fn enter(&self) -> Self {
let mut new = self.clone();
new.push(0);
new
}

fn exit(&self) -> Self {
assert!(self.len() > 0);
let mut new = self.clone();
new.pop();
new
}

fn next(&self) -> Self {
assert!(self.len() > 0);
let mut new = self.clone();
new.set(self.len() - 1, self.get(self.len() - 1).unwrap() + 1);
new
}
}

pub fn ast_to_godot_ast(ast: &Ast) -> ASTArray {
let mut collect = ASTArray::new();
let mut current_path = PathArray::top();

for statement in ast.statements() {
current_path = current_path.next();
let ast_node = statement_to_godot_ast(statement, &current_path);
collect.push(ast_node);
}

collect
}

pub fn statement_to_godot_ast(
a: &compiler::ast::Statement,
current_path: &PathArray,
) -> Gd<GodotASTNode> {
use compiler::ast::Statement;
let node = match a {
Statement::MoveRandomly() => GodotASTNode {
node_type: 1,
identifier: -1, // TODO
path: current_path.clone(),
member_data: array![],
},
Statement::MoveRelative(x, y) => GodotASTNode {
node_type: 2,
identifier: -1, // TODO
path: current_path.clone(),
member_data: varray![x, y],
},
Statement::MoveTo(x, y) => GodotASTNode {
node_type: 2,
identifier: -1, // TODO
path: current_path.clone(),
member_data: varray![x, y],
},
Statement::Calc(x) => GodotASTNode {
node_type: 3,
identifier: -1,
member_data: varray![expression_to_godot_ast(x)],
path: current_path.clone(),
member_data: varray![expression_to_godot_ast(x, current_path)], // TODO: Add path to subexpr
},
};

Gd::from_object(node)
}

pub fn expression_to_godot_ast(expr: &compiler::ast::Expression) -> Gd<GodotASTNode> {
pub fn expression_to_godot_ast(
expr: &compiler::ast::Expression,
parent_path: &PathArray,
) -> Gd<GodotASTNode> {
use compiler::ast::Expression;
let current_path = parent_path.enter();

let node = match expr {
Expression::IntLiteral(a) => GodotASTNode {
node_type: 1000,
identifier: -1,
path: current_path.clone(),
member_data: varray![a], // Literal Int
},

Expression::Addition(a, b) => GodotASTNode {
node_type: 2000,
identifier: -1,
member_data: varray![expression_to_godot_ast(a), expression_to_godot_ast(b)],
path: current_path.clone(),
member_data: varray![
expression_to_godot_ast(a, &current_path),
expression_to_godot_ast(b, &current_path.next()),
],
},

Expression::Subtraction(a, b) => GodotASTNode {
node_type: 2001,
identifier: -1,
member_data: varray![expression_to_godot_ast(a), expression_to_godot_ast(b)],
path: current_path.clone(),
member_data: varray![
expression_to_godot_ast(a, &current_path),
expression_to_godot_ast(b, &current_path.next())
],
},

Expression::Multiplication(a, b) => GodotASTNode {
node_type: 2002,
identifier: -1,
member_data: varray![expression_to_godot_ast(a), expression_to_godot_ast(b)],
path: current_path.clone(),
member_data: varray![
expression_to_godot_ast(a, &current_path),
expression_to_godot_ast(b, &current_path.next())
],
},

Expression::Division(a, b) => GodotASTNode {
node_type: 2003,
identifier: -1,
member_data: varray![expression_to_godot_ast(a), expression_to_godot_ast(b)],
path: current_path.clone(),
member_data: varray![
expression_to_godot_ast(a, &current_path),
expression_to_godot_ast(b, &current_path.next())
],
},
};

Expand Down
28 changes: 2 additions & 26 deletions rust/godot-plugin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ mod state;
use godot::builtin::Array;
use godot::prelude::*;

use compiler::Ast;
use compiler::Compilable;
use godot::engine::Engine;

use crate::conversion::ASTArray;
use crate::signals::*;
use crate::state::*;

Expand Down Expand Up @@ -61,7 +61,7 @@ impl Api {
#[func]
fn get_ast() -> ASTArray {
let ast = ast().lock().unwrap();
ast.to_godot_ast()
conversion::ast_to_godot_ast(&ast)
}

#[func]
Expand All @@ -81,27 +81,3 @@ impl Api {
ast.compile().into()
}
}

type ASTArray = Array<Gd<GodotASTNode>>;

trait ToGodotAst {
fn to_godot_ast(&self) -> ASTArray;
}

impl ToGodotAst for Ast {
fn to_godot_ast(&self) -> ASTArray {
self.statements()
.iter()
.map(conversion::to_godot_ast)
.collect::<ASTArray>()
}
}

#[allow(dead_code)]
#[derive(GodotClass)]
#[class(init, base=Node)]
struct GodotASTNode {
node_type: i64,
identifier: i64,
member_data: VariantArray,
}

0 comments on commit fe84a38

Please sign in to comment.