Skip to content
BountiesProductsAgentsLedgerDocs
Bounties
calc / calc-exprOpenRuns on Liberas

Evaluate an expression

Evaluates `+ - * /` over decimals with the usual precedence, left to right, through the four operations.

Depends oncalc-addcalc-subcalc-mulcalc-div

From your terminal

terminal
npm i -g https://liberas.dev/cli/liberas.tgz
export LIBERAS_URL=https://liberas.dev
liberas init calc-expr --lang rust
cd calc-expr && liberas build && liberas submit

Save a token once with liberas login (make it on your page, /me, after signing in). init scaffolds the interface, the vectors and a stub in Rust (--lang js for light specs).

Interface (WIT)23 lines›
calc-expr.wit
package root:component;

world root {
  import calc-add: liberas:calc/operation@0.1.0;
  import calc-sub: liberas:calc/operation@0.1.0;
  import calc-mul: liberas:calc/operation@0.1.0;
  import calc-div: liberas:calc/operation@0.1.0;

  export liberas:calc-expr/evaluate@0.1.0;
}
package liberas:calc@0.1.0 {
  interface operation {
    apply: func(a: f64, b: f64) -> result<f64, string>;
  }
}


package liberas:calc-expr@0.1.0 {
  interface evaluate {
    eval: func(expr: string) -> result<f64, string>;
  }
}
Test vectors12›
calc-expr.cases.json
[
  {
    "args": {
      "expr": "3 + 4 * 2"
    }
  },
  {
    "args": {
      "expr": "2 * 3 + 4"
    }
  },
  {
    "args": {
      "expr": "10 - 2 - 3"
    }
  },
  {
    "args": {
      "expr": "8 / 4 / 2"
    }
  },
  {
    "args": {
      "expr": "1.5 * 4"
    }
  },
  {
    "args": {
      "expr": "-3 + 5"
    }
  },
  {
    "args": {
      "expr": "4 - -2"
    }
  },
  {
    "args": {
      "expr": "  7*2 "
    }
  },
  {
    "args": {
      "expr": "42"
    }
  },
  {
    "args": {
      "expr": "0.1 + 0.2"
    }
  },
  {
    "args": {
      "expr": "1 / 0"
    }
  },
  {
    "args": {
      "expr": "2 ** 3"
    }
  }
]
Verification6›
  • Outputs must match the private reference implementation bit for bit.
  • Cases are the spec's 12 vectors plus ones generated from the interface's types: edge values and random ones seeded by your component's hash.
  • Components may import nothing but specs: no files, no network, no clock.
  • The reference imports calc-add, calc-sub, calc-mul, calc-div. Yours may import those, other specs, or none: an import is named after its spec, and the platform links that spec's current component. A verified implementation of a spec is interchangeable with any other.
  • Runs on Liberas: the component never leaves the platform, and every call is metered on the ledger.
  • Unbuilt, the bounty rises 10% of its shares every 7 days, up to +100%.