Skip to content

Commit

Permalink
make input a function, not a keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisMayfield committed Jul 12, 2024
1 parent 3e45bb2 commit 075a46d
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 27 deletions.
17 changes: 9 additions & 8 deletions src/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,10 @@ export function createExecutable(tree) {
case NODETYPES.PRINT:
return new Praxly_print(createExecutable(tree.value), tree);

case NODETYPES.INPUT:
return new Praxly_input(tree);

case NODETYPES.BUILTIN_FUNCTION_CALL: {
if (tree.name === 'random') {
if (tree.name === 'input') {
return new Praxly_input(tree);
} else if (tree.name === 'random') {
return new Praxly_random(tree);
} else if (tree.name === 'randomInt') {
return new Praxly_random_int(createExecutable(tree.parameters[0]), tree);
Expand Down Expand Up @@ -616,8 +615,9 @@ class Praxly_int_conversion {
}

async evaluate(environment) {
// this.value is likely a Praxly_String; get the actual string value
const convert = new Praxly_int(this.value.value);
let valueEvaluated = await this.value.evaluate(environment);
// value is likely a Praxly_String; get the actual string
const convert = new Praxly_int(valueEvaluated.value);
return convert;
}
}
Expand All @@ -629,8 +629,9 @@ class Praxly_float_conversion {
}

async evaluate(environment) {
// this.value is likely a Praxly_String; get the actual string value
const convert = new Praxly_float(this.value.value);
let valueEvaluated = await this.value.evaluate(environment);
// value is likely a Praxly_String; get the actual string
const convert = new Praxly_float(valueEvaluated.value);
return convert;
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/blocks2tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,10 @@ export const makeGenerator = () => {

praxlyGenerator['praxly_input_block'] = (block) => {
return {
name: 'input',
blockID: block.id,
type: NODETYPES.INPUT,
type: NODETYPES.BUILTIN_FUNCTION_CALL,
parameters: [],
};
}

Expand Down
1 change: 0 additions & 1 deletion src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ export const NODETYPES = {
...TYPES,
PRINT: "PRINT",
BUILTIN_FUNCTION_CALL: "BUILTIN_FUNCTION_CALL",
INPUT: "INPUT",
CODEBLOCK: "CODEBLOCK",
PROGRAM: "PROGRAM",
STATEMENT: "STATEMENT",
Expand Down
11 changes: 3 additions & 8 deletions src/text2tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function text2tree() {
}

/**
* This is the object that I use to tokenize the input to prepare it for parsing.
* This is the object that I use to tokenize the source code to prepare it for parsing.
*/
class Token {
constructor(type, text, line, startIndex, endIndex) {
Expand All @@ -44,8 +44,8 @@ class Lexer {
this.token_so_far = "";
this.multi_Char_symbols = ['>', '<', '=', '!', '-'];
this.symbols = [",", ";", "(", ")", "{", "}", "[", "]", ".", "+", "/", "*", "%", "^", "≠", , "←", "⟵", "≥", "≤"];
this.builtins = ['random', 'randomInt', 'randomSeed', 'int', 'float'];
this.keywords = ["if", "else", "end", "print", "input", "for", "while", 'and', 'or', 'do', 'repeat',
this.builtins = ['input', 'random', 'randomInt', 'randomSeed', 'int', 'float'];
this.keywords = ["if", "else", "end", "print", "for", "while", 'and', 'or', 'do', 'repeat',
'until', 'not', 'return', 'null'];
this.types = ['int', 'double', 'String', 'char', 'float', 'boolean', 'short', 'void'];
this.startToken = [0, 0];
Expand Down Expand Up @@ -607,11 +607,6 @@ class Parser {
this.advance();
return this.literalNode_new(this.tokens[this.i - 1]);

case 'input':
this.tokens[this.i].token_type = NODETYPES.INPUT;
this.advance();
return this.literalNode_new(this.tokens[this.i - 1]);

case NODETYPES.BUILTIN_FUNCTION_CALL:
return this.parse_builtin_function_call(line);

Expand Down
8 changes: 3 additions & 5 deletions src/tree2blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,10 @@ export const tree2blocks = (workspace, node) => {
result.getInput('EXPRESSION').connection.connect(child?.outputConnection);
break;

case NODETYPES.INPUT:
result = workspace.newBlock('praxly_input_block');
break;

case NODETYPES.BUILTIN_FUNCTION_CALL: {
if (node.name === 'random') {
if (node.name === 'input') {
result = workspace.newBlock('praxly_input_block');
} else if (node.name === 'random') {
result = workspace.newBlock('praxly_random_block');
} else if (node.name === 'randomInt') {
result = workspace.newBlock('praxly_random_int_block');
Expand Down
7 changes: 3 additions & 4 deletions src/tree2text.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,10 @@ export const tree2text = (node, indentation) => {
var expression = tree2text(node.value, node.endIndex, indentation) + '\n';
return result + expression;

case NODETYPES.INPUT:
return "input";

case NODETYPES.BUILTIN_FUNCTION_CALL: {
if (node.name === 'random') {
if (node.name === 'input') {
return "input()";
} else if (node.name === 'random') {
return "random()";
} else if (node.name === 'randomInt') {
const max = tree2text(node.parameters[0], indentation);
Expand Down

0 comments on commit 075a46d

Please sign in to comment.