mirror of
https://github.com/amyinspace/MagicSetEditor2.git
synced 2026-06-10 13:06:59 -04:00
Closure operator now behaves as default argument operator, documentation.
git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@965 0fc631ac-6414-0410-93d0-97cfa31319b6
This commit is contained in:
@@ -177,20 +177,20 @@ void KeywordReminderTextValue::highlight(const String& code, const vector<Script
|
||||
}
|
||||
|
||||
bool KeywordReminderTextValue::checkScript(const ScriptP& script) {
|
||||
Context& ctx = set.cards.empty() ? set.getContext() : set.getContext(set.cards.front());
|
||||
size_t scope = ctx.openScope();
|
||||
try {
|
||||
Context& ctx = set.cards.empty() ? set.getContext() : set.getContext(set.cards.front());
|
||||
LocalScope scope(ctx);
|
||||
for (size_t i = 0 ; i < keyword.parameters.size() ; ++i) {
|
||||
String param = String(_("param")) << (int)(i+1);
|
||||
ctx.setVariable(param, to_script(param));
|
||||
}
|
||||
script->eval(ctx);
|
||||
errors.clear();
|
||||
return true;
|
||||
} catch (const Error& e) {
|
||||
errors = e.what();
|
||||
return false;
|
||||
}
|
||||
ctx.closeScope(scope);
|
||||
return errors.empty();
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------- : Changing keywords : mode
|
||||
|
||||
+77
-24
@@ -12,6 +12,8 @@
|
||||
#include <util/error.hpp>
|
||||
#include <iostream>
|
||||
|
||||
DECLARE_TYPEOF_COLLECTION(pair<Variable COMMA ScriptValueP>);
|
||||
|
||||
// ----------------------------------------------------------------------------- : Context
|
||||
|
||||
Context::Context()
|
||||
@@ -34,6 +36,10 @@ void instrQuaternary(QuaternaryInstructionType i, ScriptValueP& a, const ScriptV
|
||||
|
||||
|
||||
ScriptValueP Context::eval(const Script& script, bool useScope) {
|
||||
if (level > 500) {
|
||||
throw ScriptError(_("Stack overflow"));
|
||||
}
|
||||
|
||||
size_t stack_size = stack.size();
|
||||
size_t scope = useScope ? openScope() : 0;
|
||||
try {
|
||||
@@ -140,7 +146,7 @@ ScriptValueP Context::eval(const Script& script, bool useScope) {
|
||||
|
||||
// Closure object
|
||||
case I_CLOSURE: {
|
||||
makeClosure(i.data);
|
||||
makeClosure(i.data, instr);
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -221,6 +227,10 @@ ScriptValueP Context::getVariable(Variable var) {
|
||||
if (variables[var].value) return variables[var].value;
|
||||
throw ScriptError(_("Variable not set: ") + variable_to_string(var));
|
||||
}
|
||||
int Context::getVariableScope(Variable var) {
|
||||
if (variables[var].value) return level - variables[var].level;
|
||||
else return -1;
|
||||
}
|
||||
|
||||
|
||||
size_t Context::openScope() {
|
||||
@@ -259,6 +269,62 @@ void instrUnary (UnaryInstructionType i, ScriptValueP& a) {
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------- : Function composition
|
||||
|
||||
/// Composition of two functions
|
||||
class ScriptCompose : public ScriptValue {
|
||||
public:
|
||||
ScriptCompose(ScriptValueP a, ScriptValueP b) : a(a), b(b) {}
|
||||
|
||||
virtual ScriptType type() const { return SCRIPT_FUNCTION; }
|
||||
virtual String typeName() const { return _("function composition"); }
|
||||
virtual ScriptValueP eval(Context& ctx) const {
|
||||
ctx.setVariable(SCRIPT_VAR_input, a->eval(ctx));
|
||||
return b->eval(ctx);
|
||||
}
|
||||
virtual ScriptValueP dependencies(Context& ctx, const Dependency& dep) const {
|
||||
ctx.setVariable(SCRIPT_VAR_input, a->dependencies(ctx, dep));
|
||||
return b->dependencies(ctx, dep);
|
||||
}
|
||||
private:
|
||||
ScriptValueP a,b;
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------- : Closures
|
||||
|
||||
/// A closure around a function
|
||||
class ScriptClosure : public ScriptValue {
|
||||
public:
|
||||
ScriptClosure(ScriptValueP fun) : fun(fun) {}
|
||||
|
||||
/// Add a binding
|
||||
void bind(Variable v, const ScriptValueP& value) {
|
||||
bindings.push_back(make_pair(v,value));
|
||||
}
|
||||
/// Apply the bindings
|
||||
void applyBindings(Context& ctx) const {
|
||||
FOR_EACH_CONST(b, bindings) {
|
||||
if (ctx.getVariableScope(b.first) != 0) {
|
||||
ctx.setVariable(b.first, b.second);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
virtual ScriptType type() const { return SCRIPT_FUNCTION; }
|
||||
virtual String typeName() const { return _("function closure"); }
|
||||
virtual ScriptValueP eval(Context& ctx) const {
|
||||
applyBindings(ctx);
|
||||
return fun->eval(ctx);
|
||||
}
|
||||
virtual ScriptValueP dependencies(Context& ctx, const Dependency& dep) const {
|
||||
applyBindings(ctx);
|
||||
return fun->dependencies(ctx, dep);
|
||||
}
|
||||
private:
|
||||
ScriptValueP fun;
|
||||
vector<pair<Variable,ScriptValueP> > bindings;
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------- : Simple instructions : binary
|
||||
|
||||
// operator on ints
|
||||
@@ -284,24 +350,6 @@ void instrUnary (UnaryInstructionType i, ScriptValueP& a) {
|
||||
} \
|
||||
break
|
||||
|
||||
/// Composition of two functions
|
||||
class ScriptCompose : public ScriptValue {
|
||||
public:
|
||||
ScriptCompose(ScriptValueP a, ScriptValueP b) : a(a), b(b) {}
|
||||
|
||||
virtual ScriptType type() const { return SCRIPT_FUNCTION; }
|
||||
virtual String typeName() const { return _("function composition"); }
|
||||
virtual ScriptValueP eval(Context& ctx) const {
|
||||
ctx.setVariable(SCRIPT_VAR_input, a->eval(ctx));
|
||||
return b->eval(ctx);
|
||||
}
|
||||
virtual ScriptValueP dependencies(Context& ctx, const Dependency& dep) const {
|
||||
ctx.setVariable(SCRIPT_VAR_input, a->dependencies(ctx, dep));
|
||||
return b->dependencies(ctx, dep);
|
||||
}
|
||||
private:
|
||||
ScriptValueP a,b;
|
||||
};
|
||||
|
||||
void instrBinary (BinaryInstructionType i, ScriptValueP& a, const ScriptValueP& b) {
|
||||
switch (i) {
|
||||
@@ -408,9 +456,14 @@ void Context::makeObject(size_t n) {
|
||||
stack.push_back(ret);
|
||||
}
|
||||
|
||||
void Context::makeClosure(size_t n) {
|
||||
//intrusive_ptr<ScriptClosure> ret(new ScriptClosure());
|
||||
// TODO
|
||||
//stack.push_back(ret);
|
||||
throw InternalError(_("TODO: makeClosure"));
|
||||
void Context::makeClosure(size_t n, const Instruction*& instr) {
|
||||
intrusive_ptr<ScriptClosure> closure(new ScriptClosure(stack[stack.size() - n - 1]));
|
||||
for (size_t j = 0 ; j < n ; ++j) {
|
||||
closure->bind((Variable)instr[n - j - 1].data, stack.back());
|
||||
stack.pop_back();
|
||||
}
|
||||
// skip arguments
|
||||
instr += n;
|
||||
// set value
|
||||
stack.back() = closure;
|
||||
}
|
||||
|
||||
@@ -62,12 +62,20 @@ class Context {
|
||||
ScriptValueP getVariable(Variable var);
|
||||
/// Get the value of a variable, returns ScriptValue() if it is not set
|
||||
inline ScriptValueP getVariableOpt(Variable var) { return variables[var].value; }
|
||||
/// In what scope was the variable set?
|
||||
/** Returns 0 for the current scope and >0 for outer scopes.
|
||||
* Returns -1 if the varible is not set
|
||||
*/
|
||||
int getVariableScope(Variable var);
|
||||
|
||||
private:
|
||||
|
||||
/// Open a new scope
|
||||
/** returns the number of shadowed binding before that scope */
|
||||
size_t openScope();
|
||||
/// Close a scope, must be passed a value from openScope
|
||||
void closeScope(size_t scope);
|
||||
friend class LocalScope;
|
||||
|
||||
public:// public for FOR_EACH
|
||||
/// Record of a variable
|
||||
@@ -102,7 +110,7 @@ class Context {
|
||||
/// Make an object with n elements, popping 2n values from the stack, and push it onto the stack
|
||||
void makeObject(size_t n);
|
||||
/// Make a closure with n arguments
|
||||
void makeClosure(size_t n);
|
||||
void makeClosure(size_t n, const Instruction*& instr);
|
||||
};
|
||||
|
||||
/// A class that creates a local scope
|
||||
|
||||
@@ -250,9 +250,9 @@ ScriptValueP Context::dependencies(const Dependency& dep, const Script& script)
|
||||
break;
|
||||
}
|
||||
|
||||
// Closure object
|
||||
// Closure object (as normal)
|
||||
case I_CLOSURE: {
|
||||
makeClosure(i.data);
|
||||
makeClosure(i.data, instr);
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
+29
-25
@@ -104,8 +104,8 @@ class TokenIterator {
|
||||
vector<ScriptParseError>& errors;
|
||||
/// Add an error message
|
||||
void add_error(const String& message);
|
||||
/// Expected some token instead of what was found
|
||||
void expected(const String& exp);
|
||||
/// Expected some token instead of what was found, possibly a matching opening bracket is known
|
||||
void expected(const String& exp, const Token* opening = nullptr);
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------- : Characters
|
||||
@@ -293,25 +293,28 @@ void TokenIterator::readStringToken() {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void TokenIterator::add_error(const String& message) {
|
||||
if (!errors.empty() && errors.back().start == pos) return; // already an error here
|
||||
// find line number
|
||||
int line_number(size_t pos, const String& input) {
|
||||
int line = 1;
|
||||
for (size_t i = 0 ; i < input.size() && i < pos ; ++i) {
|
||||
if (input.GetChar(i) == _('\n')) line++;
|
||||
}
|
||||
errors.push_back(ScriptParseError(pos, line, filename, message));
|
||||
return line;
|
||||
}
|
||||
void TokenIterator::expected(const String& expected) {
|
||||
|
||||
void TokenIterator::add_error(const String& message) {
|
||||
if (!errors.empty() && errors.back().start == pos) return; // already an error here
|
||||
// add error message
|
||||
errors.push_back(ScriptParseError(pos, line_number(pos,input), filename, message));
|
||||
}
|
||||
void TokenIterator::expected(const String& expected, const Token* opening) {
|
||||
size_t error_pos = peek(0).pos;
|
||||
if (!errors.empty() && errors.back().start == pos) return; // already an error here
|
||||
// find line number
|
||||
int line = 1;
|
||||
for (size_t i = 0 ; i < input.size() && i < error_pos ; ++i) {
|
||||
if (input.GetChar(i) == _('\n')) line++;
|
||||
// add error message
|
||||
if (opening) {
|
||||
errors.push_back(ScriptParseError(opening->pos, error_pos, line_number(opening->pos,input), filename, opening->value, expected, peek(0).value));
|
||||
} else {
|
||||
errors.push_back(ScriptParseError(error_pos, line_number(error_pos,input), filename, expected, peek(0).value));
|
||||
}
|
||||
errors.push_back(ScriptParseError(error_pos, line, filename, expected, peek(0).value));
|
||||
}
|
||||
|
||||
|
||||
@@ -385,12 +388,12 @@ ScriptP parse(const String& s, Packaged* package, bool string_mode) {
|
||||
|
||||
|
||||
// Expect a token, adds an error if it is not found
|
||||
bool expectToken(TokenIterator& input, const Char* expect, const Char* name_in_error = nullptr) {
|
||||
bool expectToken(TokenIterator& input, const Char* expect, const Token* opening = nullptr, const Char* name_in_error = nullptr) {
|
||||
Token token = input.read();
|
||||
if (token == expect) {
|
||||
return true;
|
||||
} else {
|
||||
input.expected(name_in_error ? name_in_error : expect);
|
||||
input.expected(name_in_error ? name_in_error : expect, opening);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -398,16 +401,16 @@ bool expectToken(TokenIterator& input, const Char* expect, const Char* name_in_e
|
||||
void parseExpr(TokenIterator& input, Script& script, Precedence minPrec) {
|
||||
// usually loop only once, unless we encounter newlines
|
||||
while (true) {
|
||||
const Token& token = input.read();
|
||||
Token token = input.read();
|
||||
if (token == _("(")) {
|
||||
// Parentheses = grouping for precedence of expressions
|
||||
parseOper(input, script, PREC_ALL);
|
||||
expectToken(input, _(")"));
|
||||
expectToken(input, _(")"), &token);
|
||||
} else if (token == _("{")) {
|
||||
// {} = function block. Parse a new Script
|
||||
intrusive_ptr<Script> subScript(new Script);
|
||||
parseOper(input, *subScript, PREC_ALL);
|
||||
expectToken(input, _("}"));
|
||||
expectToken(input, _("}"), &token);
|
||||
script.addInstruction(I_PUSH_CONST, subScript);
|
||||
} else if (token == _("[")) {
|
||||
// [] = list or map literal
|
||||
@@ -432,7 +435,7 @@ void parseExpr(TokenIterator& input, Script& script, Precedence minPrec) {
|
||||
t = input.peek();
|
||||
}
|
||||
}
|
||||
expectToken(input, _("]"));
|
||||
expectToken(input, _("]"), &token);
|
||||
script.addInstruction(I_MAKE_OBJECT, count);
|
||||
} else if (minPrec <= PREC_UNARY && token == _("-")) {
|
||||
parseOper(input, script, PREC_UNARY, I_UNARY, I_NEGATE); // unary negation
|
||||
@@ -538,7 +541,7 @@ void parseExpr(TokenIterator& input, Script& script, Precedence minPrec) {
|
||||
parseOper(input, script, PREC_ALL); // second, third, etc.
|
||||
script.addInstruction(I_BINARY, op);
|
||||
}
|
||||
expectToken(input, _(")"));
|
||||
expectToken(input, _(")"), &token);
|
||||
} else {
|
||||
// variable
|
||||
Variable var = string_to_variable(token.value);
|
||||
@@ -572,7 +575,7 @@ void parseOper(TokenIterator& input, Script& script, Precedence minPrec, Instruc
|
||||
// EBNF: expr = expr | expr oper expr
|
||||
// without left recursion: expr = expr (oper expr)*
|
||||
while (true) {
|
||||
const Token& token = input.read();
|
||||
Token token = input.read();
|
||||
if (token != TOK_OPER && token != TOK_NAME && token!=TOK_LPAREN &&
|
||||
!((token == TOK_STRING || token == TOK_INT || token == TOK_DOUBLE) && minPrec <= PREC_NEWLINE && token.newline)) {
|
||||
// not an operator-like token
|
||||
@@ -646,11 +649,12 @@ void parseOper(TokenIterator& input, Script& script, Precedence minPrec, Instruc
|
||||
} else {
|
||||
script.addInstruction(I_BINARY, I_MEMBER);
|
||||
}
|
||||
expectToken(input, _("]"));
|
||||
expectToken(input, _("]"), &token);
|
||||
} else if (minPrec <= PREC_FUN && token==_("(")) {
|
||||
// function call, read arguments
|
||||
vector<Variable> arguments;
|
||||
parseCallArguments(input, script, arguments);
|
||||
expectToken(input, _(")"), &token);
|
||||
// generate instruction
|
||||
script.addInstruction(I_CALL, (unsigned int)arguments.size());
|
||||
FOR_EACH(arg,arguments) {
|
||||
@@ -658,9 +662,10 @@ void parseOper(TokenIterator& input, Script& script, Precedence minPrec, Instruc
|
||||
}
|
||||
} else if (minPrec <= PREC_FUN && token==_("@")) {
|
||||
// closure call, read arguments
|
||||
expectToken(input, _("("));
|
||||
vector<Variable> arguments;
|
||||
expectToken(input, _("("));
|
||||
parseCallArguments(input, script, arguments);
|
||||
expectToken(input, _(")"), &token);
|
||||
// generate instruction
|
||||
script.addInstruction(I_CLOSURE, (unsigned int)arguments.size());
|
||||
FOR_EACH(arg,arguments) {
|
||||
@@ -676,7 +681,7 @@ void parseOper(TokenIterator& input, Script& script, Precedence minPrec, Instruc
|
||||
} else {
|
||||
parseOper(input, script, PREC_ALL, I_BINARY, I_ADD); // e
|
||||
}
|
||||
if (expectToken(input, _("}\""), _("}"))) {
|
||||
if (expectToken(input, _("}\""), &token, _("}"))) {
|
||||
parseOper(input, script, PREC_NONE); // y
|
||||
// optimize: e + "" -> e
|
||||
i = script.getInstructions().back();
|
||||
@@ -724,5 +729,4 @@ void parseCallArguments(TokenIterator& input, Script& script, vector<Variable>&
|
||||
t = input.peek();
|
||||
}
|
||||
}
|
||||
expectToken(input, _(")"));
|
||||
}
|
||||
|
||||
@@ -33,6 +33,10 @@ ScriptParseError::ScriptParseError(size_t pos, int line, const String& filename,
|
||||
: ParseError(_("Expected '") + exp + _("' instead of '") + found + _("'"))
|
||||
, start(pos), end(pos + found.size()), line(line), filename(filename)
|
||||
{}
|
||||
ScriptParseError::ScriptParseError(size_t pos1, size_t pos2, int line, const String& filename, const String& open, const String& close, const String& found)
|
||||
: ParseError(_("Expected closing '") + close + _("' for this '") + open + _("' instead of '") + found + _("'"))
|
||||
, start(pos1), end(pos2 + found.size()), line(line), filename(filename)
|
||||
{}
|
||||
String ScriptParseError::what() const {
|
||||
return String(_("(")) << (int)start << _("): ") << Error::what();
|
||||
}
|
||||
|
||||
@@ -83,6 +83,7 @@ class ScriptParseError : public ParseError {
|
||||
public:
|
||||
ScriptParseError(size_t pos, int line, const String& filename, const String& str);
|
||||
ScriptParseError(size_t pos, int line, const String& filename, const String& expected, const String& found);
|
||||
ScriptParseError(size_t pos1, size_t pos2, int line, const String& filename, const String& open, const String& close, const String& found);
|
||||
/// Position of the error
|
||||
size_t start, end;
|
||||
/// Line number of the error (the first line is 1)
|
||||
|
||||
Reference in New Issue
Block a user