aboutsummaryrefslogtreecommitdiff
path: root/src/util.rs
diff options
context:
space:
mode:
authorJJ2023-04-06 21:24:38 +0000
committerJJ2023-04-06 21:24:38 +0000
commit95ce33948581a10c2d55793b6317c7ce273f936a (patch)
tree410383a56b51af9644fa309bbe3f03d9d2368bb6 /src/util.rs
parent3188a0ec2174945a6b004db78b534f80c7927796 (diff)
rename project, write parser tests
Diffstat (limited to 'src/util.rs')
-rw-r--r--src/util.rs60
1 files changed, 60 insertions, 0 deletions
diff --git a/src/util.rs b/src/util.rs
new file mode 100644
index 0000000..9ed38d1
--- /dev/null
+++ b/src/util.rs
@@ -0,0 +1,60 @@
+#![allow(non_snake_case)]
+
+use crate::ast::*;
+
+// intentionally small: i want to run into errors
+/// assumption: the count is instantiated to zero
+pub fn unique_ident(count: &mut u8) -> String {
+ *count += 1;
+ if *count == 0 {
+ panic!("we've overflowed!");
+ } else {
+ return String::from(format!("{:X}", count));
+ }
+}
+
+pub fn Term(val: Value, kind: Type) -> Term {
+ return Term {val, kind};
+}
+
+pub fn Ann(expr: Expression, kind: Type) -> Expression {
+ return Expression::Annotation {
+ expr: Box::new(expr),
+ kind: kind
+ };
+}
+
+pub fn Const(val: Value, kind: Type) -> Expression {
+ return Expression::Constant {
+ term: Term {val, kind}
+ };
+}
+
+pub fn Var(id: &str) -> Expression {
+ return Expression::Variable {
+ id: String::from(id)
+ };
+}
+
+pub fn Abs(param: &str, func: Expression) -> Expression {
+ return Expression::Abstraction {
+ param: String::from(param),
+ func: Box::new(func),
+ };
+}
+
+pub fn App(func: Expression, arg: Expression) -> Expression {
+ return Expression::Application {
+ func: Box::new(func),
+ arg: Box::new(arg)
+ };
+}
+
+pub fn Cond(if_cond: Expression, if_then: Expression, if_else: Expression) -> Expression {
+ return Expression::Conditional {
+ if_cond: Box::new(if_cond),
+ if_then: Box::new(if_then),
+ if_else: Box::new(if_else)
+ };
+}
+