Compare commits
7 Commits
antiquated
...
9d24d48825
Author | SHA1 | Date | |
---|---|---|---|
|
9d24d48825 | ||
|
c3c515284d | ||
|
0903277b69 | ||
|
94c4dec9a9 | ||
|
42bc4f091c | ||
|
be2dcb5301 | ||
|
88029fc55a |
@ -62,6 +62,7 @@ of learning how to write a programming language.
|
||||
### General
|
||||
|
||||
http://thume.ca/2019/04/18/writing-a-compiler-in-rust/
|
||||
http://thume.ca/2019/07/14/a-tour-of-metaprogramming-models-for-generics/
|
||||
|
||||
### Type-checking
|
||||
https://skillsmatter.com/skillscasts/10868-inside-the-rust-compiler
|
||||
|
3
VISITOR_NOTES
Normal file
3
VISITOR_NOTES
Normal file
@ -0,0 +1,3 @@
|
||||
|
||||
-each terminal node in the AST requires a method on ASTVisitor
|
||||
-this can maybe be done with a macro?
|
@ -4,7 +4,7 @@
|
||||
#![feature(slice_patterns, box_patterns, box_syntax)]
|
||||
|
||||
//! `schala-lang` is where the Schala programming language is actually implemented.
|
||||
//! It defines the `Schala` type, which contains the state for a Schala REPL, and implements
|
||||
//! The crate defines the `Schala` type, which contains the state for a Schala REPL, and implements
|
||||
//! `ProgrammingLanguageInterface` and the chain of compiler passes for it.
|
||||
|
||||
extern crate itertools;
|
||||
@ -17,16 +17,10 @@ extern crate schala_repl;
|
||||
extern crate schala_lang_codegen;
|
||||
extern crate ena;
|
||||
|
||||
|
||||
macro_rules! bx {
|
||||
($e:expr) => { Box::new($e) }
|
||||
}
|
||||
|
||||
#[macro_use]
|
||||
mod util;
|
||||
#[macro_use]
|
||||
mod typechecking;
|
||||
|
||||
mod tokenizing;
|
||||
mod ast;
|
||||
mod parsing;
|
||||
@ -34,7 +28,7 @@ mod symbol_table;
|
||||
mod builtin;
|
||||
mod reduced_ast;
|
||||
mod eval;
|
||||
|
||||
mod schala;
|
||||
mod visitor;
|
||||
|
||||
pub use schala::Schala;
|
||||
|
@ -143,15 +143,11 @@
|
||||
//!
|
||||
|
||||
mod test;
|
||||
|
||||
use std::rc::Rc;
|
||||
|
||||
use crate::tokenizing::*;
|
||||
use crate::tokenizing::Kw::*;
|
||||
use crate::tokenizing::TokenKind::*;
|
||||
|
||||
use crate::ast::*;
|
||||
|
||||
use crate::builtin::{BinOp, PrefixOp};
|
||||
|
||||
/// Represents a parsing error
|
||||
|
@ -2,6 +2,10 @@ use std::collections::HashMap;
|
||||
use std::hash::Hash;
|
||||
use std::cmp::Eq;
|
||||
|
||||
macro_rules! bx {
|
||||
($e:expr) => { Box::new($e) }
|
||||
}
|
||||
|
||||
#[derive(Default, Debug)]
|
||||
pub struct ScopeStack<'a, T: 'a, V: 'a> where T: Hash + Eq {
|
||||
parent: Option<&'a ScopeStack<'a, T, V>>,
|
||||
|
57
schala-lang/language/src/visitor.rs
Normal file
57
schala-lang/language/src/visitor.rs
Normal file
@ -0,0 +1,57 @@
|
||||
use std::rc::Rc;
|
||||
|
||||
use crate::builtin::{BinOp, PrefixOp};
|
||||
use crate::ast::{Expression, ExpressionKind, InvocationArgument, Meta};
|
||||
|
||||
pub trait ExpressionKindVisitor<T> {
|
||||
fn nat_literal(&mut self, n: &u64) -> T;
|
||||
fn float_literal(&mut self, f: &f64) -> T;
|
||||
fn string_literal(&mut self, s: Rc<String>) -> T;
|
||||
fn bool_literal(&mut self, b: &bool) -> T;
|
||||
fn binexp(&mut self, op: &BinOp, lhs: &Expression, rhs: &Expression) -> T;
|
||||
fn prefix_exp(&mut self, op: &PrefixOp, arg: &Expression) -> T;
|
||||
fn value(&mut self, value_name: Rc<String>) -> T;
|
||||
fn call_expression(&mut self, f: &Expression, arguments: &Vec<Meta<InvocationArgument>>) -> T;
|
||||
}
|
||||
|
||||
pub fn dispatch<T>(input: &ExpressionKind, visitor: &mut dyn ExpressionKindVisitor<T>) -> T {
|
||||
use ExpressionKind::*;
|
||||
match input {
|
||||
NatLiteral(n) => visitor.nat_literal(n),
|
||||
FloatLiteral(f) => visitor.float_literal(f),
|
||||
StringLiteral(s) => visitor.string_literal(s.clone()),
|
||||
BoolLiteral(b) => visitor.bool_literal(b),
|
||||
BinExp(op, box lhs, box rhs) => visitor.binexp(op, lhs.node(), rhs.node()),
|
||||
PrefixExp(op, box arg) => visitor.prefix_exp(op, arg.node()),
|
||||
Value(val_name) => visitor.value(val_name.clone()),
|
||||
Call { box f, arguments } => visitor.call_expression(f.node(), arguments),
|
||||
_ => panic!()
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
struct NumberSummer;
|
||||
|
||||
impl ExpressionKindVisitor<u64> for NumberSummer {
|
||||
fn nat_literal(&mut self, n: &u64) -> u64 { n.clone() }
|
||||
fn float_literal(&mut self, f: &f64) -> u64
|
||||
fn string_literal(&mut self, s: Rc<String>) -> u64 { 0 }
|
||||
fn binexp(&mut self, op: &BinOp, lhs: &Expression, rhs: &Expression) -> u64 {
|
||||
let lhs = dispatch(&lhs.kind, self);
|
||||
let rhs = dispatch(&rhs.kind, self);
|
||||
lhs + rhs
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn yolo_swagg() {
|
||||
use super::*;
|
||||
|
||||
let mut t = NumberSummer;
|
||||
|
||||
let x = ExpressionKind::NatLiteral(4);
|
||||
|
||||
let result = dispatch(&x, &mut t);
|
||||
assert_eq!(result, 4);
|
||||
}
|
||||
*/
|
Loading…
Reference in New Issue
Block a user