Skip to content

Instantly share code, notes, and snippets.

@ssube
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ssube/08f9bd1b8e09879e84cc to your computer and use it in GitHub Desktop.
Save ssube/08f9bd1b8e09879e84cc to your computer and use it in GitHub Desktop.
flatscript
val add = (a, b) -> {
a + b;
};
val sub = (a, b) -> {
a - b;
};
val square = (a) -> {
a * a;
};
val cube = (a) -> {
square _ (a) * a;
};
val quad = (a) -> {
square _ (square _ (a));
};
# Set up some data #
val data = range.from _ (1, 7);
# (1 + 2) * (3 - 4) = 3 * -1 = -3 #
val x = add _ (data[0], (data[1] * 2) / 4) * sub _ (data[2], data[3]);
# (-3 * -3) = 9 #
val y = square _ (x);
# Comparison/flow control #
? x == y {
# if x equals y #
y = y + 1;
} !? x < y {
# else if x lt y #
y = y + 2;
} ! {
# else #
y = y - 1;
};
? (x == y) {
# if x equals y #
y = y + 1;
} !? ((x) < y) {
# else if x lt y #
y = y + 2;
} ! {
# else #
y = y - 1;
};
# Count characters from input #
# create a map of counts #
val counts = [:& 256];
# while we have input #
var str;
@ (str = in) {
# fill the buffer with char.at + "," #
var i = 0;
@ (i < str.len) {
val char = str[i];
++counts[char];
++i;
};
};
# print counts #
val print = (k, v) -> {
out = k + ": " + v;
};
counts.each _ print;
# Alias the necessary classes (imports) #
val fileClazz = alias java.nio.File;
val fileUtils = alias org.apache.commons.io.FileUtils;
val randomClazz = alias java.lang.util.Random;
# Get the filename from stdin #
val inputFile = in _ ();
# Read the file #
val data = fileUtils.readAllLines _ fileClazz.new _ inputFile;
# Print the lines #
data.each _ out;
# Short version #
(fileUtils.readAllLines _ fileClazz.new _ in _ ()).each _ out;
# Test Java interop #
val randClazz = alias java.util.Random;
val rand = randClazz.new _ ();
var i = in _ ();
@ i {
--i;
out _ rand.nextInt _ ();
};
# Reverse a string #
# while we have input #
var str;
@ (str = in) {
# create a buffer with str.len items #
val buf = [& str.len];
# fill the buffer with char.at + "," #
var i = str.len;
@ (i > 0) {
--i;
buf[i] = str[i];
};
# cat the buffer #
out = buf.join _ ();
};
grammar flatscript;
prog: (stmt? STMT_T)+;
stmt: decl | flow | loop | part | scope;
scope: SCOPE_L prog SCOPE_R;
func: parm OP_FUNC scope;
decl: decl_type ID asgn?;
/** Primary rule, needs cleanup
Matches most of the value-returning operations */
part: part bin_ops part # BinOp
| part log_ops part # LogOp
| un_ops part # UnOp
| part asgn # Assign
| part OP_CALL parm # Call
| part OP_PROP part # PropAccess
| part ALLOC_L part ALLOC_R # ArrayAccess
| con # ConstantAccess
| ID # SimpleAccess
| GROUP_L part GROUP_R # Group;
asgn: OP_ASGN (func | scope | alias | part);
flow: OP_IF part scope (OP_ELSE OP_IF part scope)* (OP_ELSE scope)?;
loop: OP_REP stmt scope;
parm: GROUP_L GROUP_R
| part
| GROUP_L part (GROUP_S part)* GROUP_R;
// Concrete rules (end up with a value)
con : arr | map | (INT | DEC | STR);
arr : ALLOC_L cap? ALLOC_R
| ALLOC_L part (GROUP_S part)* ALLOC_R;
map : ALLOC_L ALLOC_S cap? ALLOC_R
| ALLOC_L kvp (GROUP_S kvp)* ALLOC_R;
kvp : ID ALLOC_S part;
cap : OP_FILL part;
// Declaration keywords
decl_type: OP_VAR | OP_CON;
OP_VAR: 'var';
OP_CON: 'val';
// Java interop
alias: OP_ALIAS ID (OP_PROP ID)*;
OP_ALIAS: 'alias';
// Special operators
OP_ASGN: '=';
OP_FUNC: '->';
OP_CALL: '_';
OP_PROP: '.';
// Logical operators
log_ops: OP_EQ | OP_AND | OP_OR | OP_LESS | OP_GREAT | OP_GE | OP_LE;
OP_EQ: '==';
OP_AND: '&&';
OP_OR: '||';
OP_LESS: '<';
OP_GREAT: '>';
OP_GE: '>=';
OP_LE: '<=';
OP_IF: '?';
OP_ELSE: '!';
// Loop operators
OP_REP: '@';
OP_FILL: '&';
// General operators
un_ops: OP_SUB | OP_INC | OP_DEC;
bin_ops: OP_ADD | OP_SUB | OP_MUL | OP_DIV | OP_MOD | OP_EXP;
OP_ADD: '+';
OP_SUB: '-';
OP_MUL: '*';
OP_DIV: '/';
OP_MOD: '%';
OP_EXP: '^';
OP_INC: '++';
OP_DEC: '--';
// Grouping/scope terminals
GROUP_L: '(';
GROUP_S: ',';
GROUP_R: ')';
SCOPE_L: '{';
SCOPE_R: '}';
ALLOC_L: '[';
ALLOC_R: ']';
ALLOC_S: ':';
STMT_T : ';';
INT: '0' | [1-9][0-9]*;
DEC: [0-9]+ '.' [0-9]*;
STR: '"' (~('"' | '\\') | '\\' ('"' | '\\'))* '"';
CMT: '#' ~('#')* '#' -> skip;
ID : [A-Za-z]+;
WS : [ \t\r\n]+ -> skip ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment