Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for unary minus values #149

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions web/mygame/scenes/choicescript_stats.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ This is a stats screen!
Weakness
text Leadership
text Strength

1 change: 0 additions & 1 deletion web/mygame/scenes/startup.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

*create leadership 50
*create strength 50

Welcome to your very first ChoiceScript game!

Copyright 2010 by Dan Fabulich.
Expand Down
14 changes: 11 additions & 3 deletions web/scene.js
Original file line number Diff line number Diff line change
Expand Up @@ -1507,14 +1507,21 @@ Scene.prototype.abort = function() {
// *create
// create a new permanent stat
Scene.prototype.create = function create(line) {
var result = /^(\w*)(.*)/.exec(line);
var result = /^(\w*)\s*(-?\d*\.?\d*)(.*)/.exec(line);
if (!result) throw new Error(this.lineMsg() + "Invalid create instruction, no variable specified: " + line);
var variable = result[1];
this.validateVariable(variable);
variable = variable.toLowerCase();
var expr = result[2];
var expr = result[2] + result[3]; // Combine the number (possibly negative) with the rest of the expression
var stack = this.tokenizeExpr(expr);
if (stack.length > 1) throw new Error(this.lineMsg() + "Invalid create instruction, too many values: " + line);
if (stack.length > 1) {
if (['+','-'].includes(stack[0].value) && stack[1].name == "NUMBER") {
if (stack[0].value == '-') stack[1].value = "-" + stack[1].value;
stack.shift();
}else{
throw new Error(this.lineMsg() + "Invalid create instruction, too many values: " + line)
}
};
this.createVariable(variable, stack[0], line);
}

Expand All @@ -1527,6 +1534,7 @@ Scene.prototype.createVariable = function createVariable(variable, token, line)
if (!/STRING|NUMBER|VAR/.test(token.name)) complexError();
if ("VAR" == token.name && !/^true|false$/i.test(token.value)) complexError();
if ("STRING" == token.name && /(\$|@)!?!?{/.test(token.value)) throw new Error(this.lineMsg() + "Invalid create instruction, value must be a simple string without ${} or @{}: " + line);
if ("NUMBER" == token.name && !/^[-+]?\d*\.?\d+$/.test(token.value)) complexError(); // Adjusted regex to include negative numbers
var value = this.evaluateExpr([token]);
if (!this.created) this.created = {};
if (this.created[variable]) throw new Error(this.lineMsg() + "Invalid create. " + variable + " was previously created on line " + this.created[variable]);
Expand Down