1.0.0-alpha.0 • Published 7 years ago
@frograming/language v1.0.0-alpha.0
@frograming/language
This repo defines the language
.frog.
Install
npm install @frograming/languageIntroduction
Frogram is a simple programing language that compiles to "frogger command".
A frogger command is a String of this format: Move(Up|Right|Down|Left) | NO_OP.
This package exposes two methods: parse and interpret.
parsetakes in a frogram code, returning the abstract syntax tree (AST) of the code.interprettakes in the AST, returning anexecuationobject.
The execution object is a simple object representing the execution context:
execution.commands- Type: <
Array<String>> - Description: Commands that are returned from
tick.
- Type: <
execution.tick(state)- Type: <
Function> - Arguments:
state: <Object>, an object representing the frogger state- A frogger state is an object with
isGoalUp|isRiverUp|is(Log|Car|Wall)(Up|Right|Down|Left)keys and boolean values. - e.g.
{ isLogUp: false, isCarDown: true ... }
- A frogger state is an object with
- returns: <
String>, a frogger command:Move(Up|Right|Down|Left) | NO_OP.
- Description: Use the given state to run the frogCode the program until an
execstatement.
- Type: <
Language Introduction
WIP
Usage
import { parse, interpret } '@frograming/language';
const ast = parse(`onTick {
if (isGoalUp()) {
exec moveUp;
}
if (!isCarUp()) {
exec moveUp;
} else {
if (isCarLeft()) {
exec moveRight;
}
if (isCarRight()) {
exec moveLeft;
exec moveRight;
}
}
}`);
const execuation = interpret(ast);
const command = execuation.tick({
isGoalUp: true,
isCarUp: false,
isCarLeft: true
...
});
// command === 'moveUp';Author
Jason Yu