1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
|
// nolint: golint, dupl
package main
import (
"fmt"
"io"
"math"
"github.com/alecthomas/repr"
"github.com/alecthomas/participle/v2"
)
type Evaluatable interface {
Evaluate(ctx *Context) (interface{}, error)
}
type Function func(args ...interface{}) (interface{}, error)
// Context for evaluation.
type Context struct {
// User-provided functions.
Functions map[string]Function
// Vars defined during evaluation.
Vars map[string]interface{}
// Reader from which INPUT is read.
Input io.Reader
// Writer where PRINTing will write.
Output io.Writer
}
func (p *Program) init() {
p.Table = map[int]*Command{}
for index, cmd := range p.Commands {
cmd.Index = index
p.Table[cmd.Line] = cmd
}
}
func (v *Value) Evaluate(ctx *Context) (interface{}, error) {
switch {
case v.Number != nil:
return *v.Number, nil
case v.String != nil:
return *v.String, nil
case v.Variable != nil:
value, ok := ctx.Vars[*v.Variable]
if !ok {
return nil, fmt.Errorf("unknown variable %q", *v.Variable)
}
return value, nil
case v.Subexpression != nil:
return v.Subexpression.Evaluate(ctx)
case v.Call != nil:
return v.Call.Evaluate(ctx)
}
panic("unsupported value type" + repr.String(v))
}
func (f *Factor) Evaluate(ctx *Context) (interface{}, error) {
base, err := f.Base.Evaluate(ctx)
if err != nil {
return nil, err
}
if f.Exponent == nil {
return base, nil
}
baseNum, exponentNum, err := evaluateFloats(ctx, base, f.Exponent)
if err != nil {
return nil, participle.Errorf(f.Pos, "invalid factor: %s", err)
}
return math.Pow(baseNum, exponentNum), nil
}
func (o *OpFactor) Evaluate(ctx *Context, lhs interface{}) (interface{}, error) {
lhsNumber, rhsNumber, err := evaluateFloats(ctx, lhs, o.Factor)
if err != nil {
return nil, participle.Errorf(o.Pos, "invalid arguments for %s: %s", o.Operator, err)
}
switch o.Operator {
case "*":
return lhsNumber * rhsNumber, nil
case "/":
return lhsNumber / rhsNumber, nil
}
panic("unreachable")
}
func (t *Term) Evaluate(ctx *Context) (interface{}, error) {
lhs, err := t.Left.Evaluate(ctx)
if err != nil {
return nil, err
}
for _, right := range t.Right {
rhs, err := right.Evaluate(ctx, lhs)
if err != nil {
return nil, err
}
lhs = rhs
}
return lhs, nil
}
func (o *OpTerm) Evaluate(ctx *Context, lhs interface{}) (interface{}, error) {
lhsNumber, rhsNumber, err := evaluateFloats(ctx, lhs, o.Term)
if err != nil {
return nil, participle.Errorf(o.Pos, "invalid arguments for %s: %s", o.Operator, err)
}
switch o.Operator {
case "+":
return lhsNumber + rhsNumber, nil
case "-":
return lhsNumber - rhsNumber, nil
}
panic("unreachable")
}
func (c *Cmp) Evaluate(ctx *Context) (interface{}, error) {
lhs, err := c.Left.Evaluate(ctx)
if err != nil {
return nil, err
}
for _, right := range c.Right {
rhs, err := right.Evaluate(ctx, lhs)
if err != nil {
return nil, err
}
lhs = rhs
}
return lhs, nil
}
func (o *OpCmp) Evaluate(ctx *Context, lhs interface{}) (interface{}, error) {
rhs, err := o.Cmp.Evaluate(ctx)
if err != nil {
return nil, err
}
switch lhs := lhs.(type) {
case float64:
rhs, ok := rhs.(float64)
if !ok {
return nil, participle.Errorf(o.Pos, "rhs of %s must be a number", o.Operator)
}
switch o.Operator {
case "=":
return lhs == rhs, nil
case "!=":
return lhs != rhs, nil
case "<":
return lhs < rhs, nil
case ">":
return lhs > rhs, nil
case "<=":
return lhs <= rhs, nil
case ">=":
return lhs >= rhs, nil
}
case string:
rhs, ok := rhs.(string)
if !ok {
return nil, participle.Errorf(o.Pos, "rhs of %s must be a string", o.Operator)
}
switch o.Operator {
case "=":
return lhs == rhs, nil
case "!=":
return lhs != rhs, nil
case "<":
return lhs < rhs, nil
case ">":
return lhs > rhs, nil
case "<=":
return lhs <= rhs, nil
case ">=":
return lhs >= rhs, nil
}
default:
return nil, participle.Errorf(o.Pos, "lhs of %s must be a number or string", o.Operator)
}
panic("unreachable")
}
func (e *Expression) Evaluate(ctx *Context) (interface{}, error) {
lhs, err := e.Left.Evaluate(ctx)
if err != nil {
return nil, err
}
for _, right := range e.Right {
rhs, err := right.Evaluate(ctx, lhs)
if err != nil {
return nil, err
}
lhs = rhs
}
return lhs, nil
}
func (c *Call) Evaluate(ctx *Context) (interface{}, error) {
function, ok := ctx.Functions[c.Name]
if !ok {
return nil, participle.Errorf(c.Pos, "unknown function %q", c.Name)
}
args := []interface{}{}
for _, arg := range c.Args {
value, err := arg.Evaluate(ctx)
if err != nil {
return nil, err
}
args = append(args, value)
}
value, err := function(args...)
if err != nil {
return nil, participle.Errorf(c.Pos, "call to %s() failed", c.Name)
}
return value, nil
}
func (p *Program) Evaluate(r io.Reader, w io.Writer, functions map[string]Function) error {
if len(p.Commands) == 0 {
return nil
}
ctx := &Context{
Vars: map[string]interface{}{},
Functions: functions,
Input: r,
Output: w,
}
for index := 0; index < len(p.Commands); {
cmd := p.Commands[index]
switch {
case cmd.Goto != nil:
cmd := cmd.Goto
next, ok := p.Table[cmd.Line]
if !ok {
return participle.Errorf(cmd.Pos, "invalid line number %d", cmd.Line)
}
index = next.Index
continue
case cmd.Remark != nil:
case cmd.Let != nil:
cmd := cmd.Let
value, err := cmd.Value.Evaluate(ctx)
if err != nil {
return err
}
ctx.Vars[cmd.Variable] = value
case cmd.Print != nil:
cmd := cmd.Print
value, err := cmd.Expression.Evaluate(ctx)
if err != nil {
return err
}
fmt.Fprintln(ctx.Output, value)
case cmd.Input != nil:
cmd := cmd.Input
var value float64
_, err := fmt.Fscanln(ctx.Input, &value)
if err != nil {
return participle.Errorf(cmd.Pos, "invalid input: %s", err)
}
ctx.Vars[cmd.Variable] = value
case cmd.If != nil:
cmd := cmd.If
condition, err := cmd.Condition.Evaluate(ctx)
if err != nil {
return err
}
if test, ok := condition.(bool); ok && test {
next, ok := p.Table[cmd.Line]
if !ok {
return participle.Errorf(cmd.Pos, "invalid line number %d", cmd.Line)
}
index = next.Index
continue
}
case cmd.Call != nil:
_, err := cmd.Call.Evaluate(ctx)
if err != nil {
return err
}
default:
panic("unsupported command " + repr.String(cmd))
}
index++
}
return nil
}
func evaluateFloats(ctx *Context, lhs interface{}, rhsExpr Evaluatable) (float64, float64, error) {
rhs, err := rhsExpr.Evaluate(ctx)
if err != nil {
return 0, 0, err
}
lhsNumber, ok := lhs.(float64)
if !ok {
return 0, 0, fmt.Errorf("lhs must be a number")
}
rhsNumber, ok := rhs.(float64)
if !ok {
return 0, 0, fmt.Errorf("rhs must be a number")
}
return lhsNumber, rhsNumber, nil
}
|