Skip to content

Commit

Permalink
Update ast_eval.dart
Browse files Browse the repository at this point in the history
Extended sum operator to work on strings
  • Loading branch information
LucaBoss74 authored Sep 16, 2021
1 parent 4dc3b69 commit 9d01a77
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/ast_eval.dart
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,14 @@ abstract class Expr {
bool get assignable => false;

// default arithmetic & bit operations
static SmyValue add(SmyValue l, SmyValue r) => SmyNum(l.numValue + r.numValue);
static SmyValue add(SmyValue l, SmyValue r) {
if (l is SmyNum && r is SmyNum)
return SmyNum(l.numValue + r.numValue);
if (l is SmyString && r is SmyString)
return SmyString(l.value + r.value);
else
throw "TypeError: left and right operand should be num or string for + to work";
}
static SmyValue sub(SmyValue l, SmyValue r) => SmyNum(l.numValue - r.numValue);
static SmyValue mul(SmyValue l, SmyValue r) => SmyNum(l.numValue * r.numValue);
static SmyValue div(SmyValue l, SmyValue r) => SmyNum(l.numValue / r.numValue);
Expand Down

0 comments on commit 9d01a77

Please sign in to comment.