Skip to content

Commit

Permalink
implemented functional pipeline operators
Browse files Browse the repository at this point in the history
  • Loading branch information
katsaii committed Aug 8, 2023
1 parent ac947ca commit 76e94b9
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ environment = new CatspeakEnvironment();
environment.applyPreset(CatspeakPreset.MATH, CatspeakPreset.DRAW);
environment.addFunction(
"get_timer", get_timer,
"get_room_width", function () { return room_width },
"get_room_height", function () { return room_height },
"room_width", function () { return room_width },
"room_height", function () { return room_height },
);

code = @'
Expand All @@ -16,8 +16,8 @@ code = @'
let t = get_timer() / 5000;
let angle = 270 + 30 * dsin(t);
let w = get_room_width();
let h = get_room_height();
let w = :room_width;
let h = :room_height;
let ox = w / 2;
let oy = 0;
Expand Down
6 changes: 6 additions & 0 deletions src-lts/scripts/scr_catspeak_lexer/scr_catspeak_lexer.gml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ enum CatspeakToken {
AND,
/// The logical `or` operator.
OR,
/// The functional pipe right `|>` operator.
PIPE_RIGHT,
/// The functional pipe left `<|` operator.
PIPE_LEFT,
/// The `do` keyword.
DO,
/// The `if` keyword.
Expand Down Expand Up @@ -779,6 +783,8 @@ function __catspeak_keywords_create() {
keywords[$ "|"] = CatspeakToken.BITWISE_OR;
keywords[$ "and"] = CatspeakToken.AND;
keywords[$ "or"] = CatspeakToken.OR;
keywords[$ "|>"] = CatspeakToken.PIPE_RIGHT;
keywords[$ "<|"] = CatspeakToken.PIPE_LEFT;
keywords[$ "do"] = CatspeakToken.DO;
keywords[$ "if"] = CatspeakToken.IF;
keywords[$ "else"] = CatspeakToken.ELSE;
Expand Down
29 changes: 26 additions & 3 deletions src-lts/scripts/scr_catspeak_parser/scr_catspeak_parser.gml
Original file line number Diff line number Diff line change
Expand Up @@ -243,25 +243,48 @@ function CatspeakParser(lexer, builder) constructor {
///
/// @return {Struct}
static __parseOpLogical = function () {
var result = __parseOpBitwise();
var result = __parseOpPipe();
while (true) {
var peeked = lexer.peek();
if (peeked == CatspeakToken.AND) {
lexer.next();
var lhs = result;
var rhs = __parseOpBitwise();
var rhs = __parseOpPipe();
result = asg.createAnd(op, lhs, rhs, lexer.getLocation());
} else if (peeked == CatspeakToken.OR) {
lexer.next();
var lhs = result;
var rhs = __parseOpBitwise();
var rhs = __parseOpPipe();
result = asg.createOr(op, lhs, rhs, lexer.getLocation());
} else {
return result;
}
}
};

/// @ignore
///
/// @return {Struct}
static __parseOpPipe = function () {
var result = __parseOpBitwise();
while (true) {
var peeked = lexer.peek();
if (peeked == CatspeakToken.PIPE_RIGHT) {
lexer.next();
var lhs = result;
var rhs = __parseOpBitwise();
result = asg.createCall(rhs, [lhs], lexer.getLocation());
} else if (peeked == CatspeakToken.PIPE_LEFT) {
lexer.next();
var lhs = result;
var rhs = __parseOpBitwise();
result = asg.createCall(lhs, [rhs], lexer.getLocation());
} else {
return result;
}
}
};

/// @ignore
///
/// @return {Struct}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,4 +237,22 @@ test_add(function () : Test("engine-properties-get-set") constructor {
var result = f();
assertEq(2 * 8, result[0]);
assertEq(2 * (2 * 8 + 6) + 2 * (2 * 8 + 6), result[1]);
});

test_add(function () : Test("engine-pipe-left") constructor {
var engine = new CatspeakEnvironment();
var f = engine.compileGML(engine.parseString(@'
let f = fun (x) { return x * 2 }
f <| 7
'));
assertEq(7 * 2, f());
});

test_add(function () : Test("engine-pipe-right") constructor {
var engine = new CatspeakEnvironment();
var f = engine.compileGML(engine.parseString(@'
let f = fun (x) { return x + "world" }
"hello" |> f
'));
assertEq("helloworld", f());
});

0 comments on commit 76e94b9

Please sign in to comment.