schala/schala-lang/language/src/ast/walker.rs

113 lines
2.6 KiB
Rust

use std::rc::Rc;
use crate::ast::*;
use crate::ast::visitor::ASTVisitor;
pub fn ast<V: ASTVisitor>(v: &mut V, ast: &AST) {
v.block(&ast.statements);
}
pub fn block<V: ASTVisitor>(v: &mut V, block: &Vec<Statement>) {
for statement in block {
v.statement(statement);
}
}
pub fn statement<V: ASTVisitor>(v: &mut V, statement: &Statement) {
use StatementKind::*;
match statement.kind {
Expression(ref expr) => v.expression(expr),
Declaration(ref decl) => v.declaration(decl),
Import(ref import_spec) => v.import(import_spec),
}
}
pub fn declaration<V: ASTVisitor>(v: &mut V, decl: &Declaration) {
use Declaration::*;
match decl {
FuncSig(sig) => {
v.signature(&sig);
},
FuncDecl(sig, block) => {
v.signature(&sig);
v.block(&block);
},
TypeDecl { .. } => unimplemented!(),
TypeAlias(_, _) => unimplemented!(),
Binding { name, constant, type_anno, expr } => {
v.binding(name, *constant, type_anno.as_ref(), expr);
},
/*
Impl {
type_name: TypeIdentifier,
interface_name: Option<TypeSingletonName>,
block: Vec<Declaration>,
},
Interface {
name: Rc<String>,
signatures: Vec<Signature>
}
*/
_ => (),
}
}
pub fn expression<V: ASTVisitor>(v: &mut V, expression: &Expression) {
v.expression_kind(&expression.kind);
v.maybe_type_identifier(expression.type_anno.as_ref());
}
pub fn maybe_type_identifier<V: ASTVisitor>(v: &mut V, maybe_ty_identifier: Option<&TypeIdentifier>) {
}
pub fn expression_kind<V: ASTVisitor>(v: &mut V, expression_kind: &ExpressionKind) {
use ExpressionKind::*;
match expression_kind {
NatLiteral(n) => v.nat_literal(*n),
FloatLiteral(f) => v.float_literal(*f),
StringLiteral(s) => v.string_literal(s),
BoolLiteral(b) => v.bool_literal(*b),
BinExp(op, lhs, rhs) => v.binexp(op, lhs, rhs),
PrefixExp(op, arg) => v.prefix_exp(op, arg),
TupleLiteral(exprs) => {
for expr in exprs {
v.expression(expr);
}
},
Value(name) => v.qualified_name(name),
NamedStruct { name, fields } => v.named_struct(name, fields),
_ => (),
}
/*
Call {
f: Box<Expression>,
arguments: Vec<InvocationArgument>,
},
Index {
indexee: Box<Expression>,
indexers: Vec<Expression>,
},
IfExpression {
discriminator: Box<Discriminator>,
body: Box<IfExpressionBody>,
},
WhileExpression {
condition: Option<Box<Expression>>,
body: Block,
},
ForExpression {
enumerators: Vec<Enumerator>,
body: Box<ForBody>,
},
Lambda {
params: Vec<FormalParam>,
type_anno: Option<TypeIdentifier>,
body: Block,
},
ListLiteral(Vec<Expression>),
}
*/
}