From d3866a190821d3bc1af3c68e92d98c533615fbf8 Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Tue, 23 Nov 2021 23:56:25 -0800 Subject: [PATCH] Self --- schala-lang/src/ast/mod.rs | 1 + schala-lang/src/ast/visitor.rs | 7 ++++++- schala-lang/src/ast/visualize.rs | 1 + schala-lang/src/parsing/combinator.rs | 5 +++++ schala-lang/src/reduced_ir/mod.rs | 1 + 5 files changed, 14 insertions(+), 1 deletion(-) diff --git a/schala-lang/src/ast/mod.rs b/schala-lang/src/ast/mod.rs index eb85983..2a9c8ac 100644 --- a/schala-lang/src/ast/mod.rs +++ b/schala-lang/src/ast/mod.rs @@ -208,6 +208,7 @@ pub enum ExpressionKind { PrefixExp(PrefixOp, Box), TupleLiteral(Vec), Value(QualifiedName), + SelfValue, NamedStruct { name: QualifiedName, fields: Vec<(Rc, Expression)> }, Call { f: Box, arguments: Vec }, Index { indexee: Box, indexers: Vec }, diff --git a/schala-lang/src/ast/visitor.rs b/schala-lang/src/ast/visitor.rs index ca6e5e5..d481e3c 100644 --- a/schala-lang/src/ast/visitor.rs +++ b/schala-lang/src/ast/visitor.rs @@ -71,7 +71,12 @@ pub fn walk_expression(v: &mut V, expr: &Expression) { if let Recursion::Continue = v.expression(expr) { match &expr.kind { - NatLiteral(_) | FloatLiteral(_) | StringLiteral { .. } | BoolLiteral(_) | Value(_) => (), + NatLiteral(_) + | FloatLiteral(_) + | StringLiteral { .. } + | BoolLiteral(_) + | Value(_) + | SelfValue => (), BinExp(_, lhs, rhs) => { walk_expression(v, lhs); walk_expression(v, rhs); diff --git a/schala-lang/src/ast/visualize.rs b/schala-lang/src/ast/visualize.rs index e9bca2f..c14b5b4 100644 --- a/schala-lang/src/ast/visualize.rs +++ b/schala-lang/src/ast/visualize.rs @@ -45,6 +45,7 @@ fn render_expression(expr: &Expression, indent: usize, buf: &mut String) { buf.push_str("(Expr "); match &expr.kind { + SelfValue => write!(buf, "(SelfValue)").unwrap(), NatLiteral(n) => buf.push_str(&format!("(NatLiteral {})", n)), FloatLiteral(f) => buf.push_str(&format!("(FloatLiteral {})", f)), StringLiteral { s, prefix } => buf.push_str(&format!("(StringLiteral prefix: {:?} {})", prefix, s)), diff --git a/schala-lang/src/parsing/combinator.rs b/schala-lang/src/parsing/combinator.rs index 68d3ec0..7cd03b3 100644 --- a/schala-lang/src/parsing/combinator.rs +++ b/schala-lang/src/parsing/combinator.rs @@ -617,6 +617,7 @@ fn primary_expr_no_struct(input: Span) -> ParseResult { float_literal, number_literal, string_literal, + self_expr, )), )(input) } @@ -631,6 +632,10 @@ fn named_struct(input: Span) -> ParseResult { )(input) } +fn self_expr(input: Span) -> ParseResult { + context("self-expression", map(kw("self"), |_| ExpressionKind::SelfValue))(input) +} + //TODO support anonymous structs and Elm-style update syntax for structs fn record_block(input: Span) -> ParseResult, Expression)>> { let record_entry = diff --git a/schala-lang/src/reduced_ir/mod.rs b/schala-lang/src/reduced_ir/mod.rs index 182d55f..e45d7bf 100644 --- a/schala-lang/src/reduced_ir/mod.rs +++ b/schala-lang/src/reduced_ir/mod.rs @@ -124,6 +124,7 @@ impl<'a, 'b> Reducer<'a, 'b> { use crate::ast::ExpressionKind::*; match &expr.kind { + SelfValue => panic!(), NatLiteral(n) => Expression::Literal(Literal::Nat(*n)), FloatLiteral(f) => Expression::Literal(Literal::Float(*f)), //TODO implement handling string literal prefixes