Skip to content

Commit

Permalink
added a pipeline operator.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kray-G committed Feb 16, 2021
1 parent 0c43289 commit 5435287
Show file tree
Hide file tree
Showing 5 changed files with 1,220 additions and 1,184 deletions.
19 changes: 19 additions & 0 deletions examples/pipeline.kx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const double = &(n) => n * 2;
const increment = &(n) => n + 1;

// Normal case.
var r1 = double(increment(double(double(5)))); // 42
System.println(r1);

// Rewrite it with a pipeline operator.
var r2 = 5 |> double |> double |> increment |> double; // 42
System.println(r2);

class Person(name) {
public said(x) {
'"%{x}," %{name} said.' |> System.println;
}
}

var john = new Person("John");
"Thank you very much" |> john.said;
113 changes: 57 additions & 56 deletions include/parser.tab.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,59 +52,60 @@ extern YYSTYPE kx_yylval;
#define SHR 292
#define POW 293
#define LUNDEF 294
#define ADDEQ 295
#define SUBEQ 296
#define MULEQ 297
#define DIVEQ 298
#define MODEQ 299
#define ANDEQ 300
#define OREQ 301
#define XOREQ 302
#define LANDEQ 303
#define LOREQ 304
#define LUNDEFEQ 305
#define SHLEQ 306
#define SHREQ 307
#define REGEQ 308
#define REGNE 309
#define NUL 310
#define TRUE 311
#define FALSE 312
#define AS 313
#define IMPORT 314
#define USING 315
#define DARROW 316
#define SQ 317
#define DQ 318
#define MLSTR 319
#define BINEND 320
#define DOTS2 321
#define DOTS3 322
#define REGPF 323
#define NAMESPACE 324
#define SYSNS 325
#define SYSRET_NV 326
#define CLASS 327
#define SYSCLASS 328
#define MODULE 329
#define SYSMODULE 330
#define NATIVE 331
#define FUNCTION 332
#define SYSFUNC 333
#define PUBLIC 334
#define PRIVATE 335
#define PROTECTED 336
#define COROUTINE 337
#define NAME 338
#define STR 339
#define SRCFILE 340
#define BIGINT 341
#define INT 342
#define TYPE 343
#define TYPEOF 344
#define LBBR 345
#define RBBR 346
#define LMBR 347
#define RMBR 348
#define DBL 349
#define BIN 350
#define PIPEOP 295
#define ADDEQ 296
#define SUBEQ 297
#define MULEQ 298
#define DIVEQ 299
#define MODEQ 300
#define ANDEQ 301
#define OREQ 302
#define XOREQ 303
#define LANDEQ 304
#define LOREQ 305
#define LUNDEFEQ 306
#define SHLEQ 307
#define SHREQ 308
#define REGEQ 309
#define REGNE 310
#define NUL 311
#define TRUE 312
#define FALSE 313
#define AS 314
#define IMPORT 315
#define USING 316
#define DARROW 317
#define SQ 318
#define DQ 319
#define MLSTR 320
#define BINEND 321
#define DOTS2 322
#define DOTS3 323
#define REGPF 324
#define NAMESPACE 325
#define SYSNS 326
#define SYSRET_NV 327
#define CLASS 328
#define SYSCLASS 329
#define MODULE 330
#define SYSMODULE 331
#define NATIVE 332
#define FUNCTION 333
#define SYSFUNC 334
#define PUBLIC 335
#define PRIVATE 336
#define PROTECTED 337
#define COROUTINE 338
#define NAME 339
#define STR 340
#define SRCFILE 341
#define BIGINT 342
#define INT 343
#define TYPE 344
#define TYPEOF 345
#define LBBR 346
#define RBBR 347
#define LMBR 348
#define RMBR 349
#define DBL 350
#define BIN 351
15 changes: 8 additions & 7 deletions src/kinx.y
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ static inline void yy_restart(int token)
%token ERROR
%token IF ELSE WHILE DO FOR IN TRY CATCH FINALLY BREAK CONTINUE SWITCH CASE DEFAULT WHEN ENUM FALLTHROUGH
%token NEW VAR CONST RETURN THROW YIELD MIXIN
%token EQEQ NEQ LE GE LGE LOR LAND INC DEC SHL SHR POW LUNDEF
%token EQEQ NEQ LE GE LGE LOR LAND INC DEC SHL SHR POW LUNDEF PIPEOP
%token ADDEQ SUBEQ MULEQ DIVEQ MODEQ ANDEQ OREQ XOREQ LANDEQ LOREQ LUNDEFEQ SHLEQ SHREQ REGEQ REGNE
%token NUL TRUE FALSE AS
%token IMPORT USING DARROW SQ DQ MLSTR BINEND DOTS2 DOTS3 REGPF NAMESPACE SYSNS SYSRET_NV
Expand Down Expand Up @@ -95,7 +95,6 @@ static inline void yy_restart(int token)
%type<obj> ObjectSpecialSyntax
%type<obj> AssignExpressionList_Opt
%type<obj> TernaryExpression
%type<obj> FunctionExpression
%type<obj> LogicalUndefExpression
%type<obj> LogicalOrExpression
%type<obj> LogicalAndExpression
Expand Down Expand Up @@ -161,6 +160,7 @@ static inline void yy_restart(int token)
%type<intval> ArrayLevel
%type<intval> ReturnType_Opt
%type<intval> GetLineNumber
%type<obj> PipelineExpression
%type<obj> CaseWhenExpression
%type<obj> WhenClauseList
%type<obj> WhenClause
Expand Down Expand Up @@ -536,13 +536,14 @@ Colon_Opt
;

TernaryExpression
: FunctionExpression
| LogicalUndefExpression '?' TernaryExpression ':' TernaryExpression { $$ = kx_gen_texpr_object($1, $3, $5); }
: AnonymousFunctionDeclExpression
| PipelineExpression
| PipelineExpression '?' TernaryExpression ':' TernaryExpression { $$ = kx_gen_texpr_object($1, $3, $5); }
;

FunctionExpression
: AnonymousFunctionDeclExpression
| LogicalUndefExpression
PipelineExpression
: LogicalUndefExpression
| PipelineExpression PIPEOP LogicalUndefExpression { $$ = kx_gen_bexpr_object(KXOP_CALL, $3, $1); }
;

LogicalUndefExpression
Expand Down
4 changes: 4 additions & 0 deletions src/lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,10 @@ int kx_yylex()
kx_lex_next(kx_lexinfo);
return OREQ;
}
if (kx_lexinfo.ch == '>') {
kx_lex_next(kx_lexinfo);
return PIPEOP;
}
return '|';
case '&':
kx_lex_next(kx_lexinfo);
Expand Down

0 comments on commit 5435287

Please sign in to comment.