gocalc

interactive calculator

features

  • float numbers, basic operators(+, -, *, /, (, ))
  • enhanced error handling with indication of problem position in input
  • tokenizer is enough smart to proccess arbitrary formatted expressions
  • parser upgraded to work with unary operators
  • variables
  • simple functions (one line formulas, without subcalls)
  • meta commands(show something and etc…)
  • script mode, options
  • uses readline library(interactive editing)
  • autocomplete(functions and variables)
  • can create config file with predefined functions and variables
  • optimize(minimize) function expressions
  • api to interact with interpreter objects from go code

testing

go test .

building

go build -o calc ./cmd
./calc – to run

quick run

go run ./cmd

syntax

  • identifier: starts with letter, can consist of letters and digits(case-sensetive)

  • number: floating point number (dot as fraction separator)

  • variable:

    • variable_name: identifier
    • assignment: varable_name = expression variable variable_name with value of expression
      • example: var = 2 * 2 variable var with value 4
    • usage: variable_name => gives value of variable variable_name
      • example: var => 4
  • function:

    • function_name: @identifier
    • declaration: function_name = (variable_name [,variable_name]): expression function with name function_name with zero or more parameters(separated with comma), that used for calculate expression
      • example: @foo = (a, b): 2 * a - b
    • usage: function_name(expression [,expression]) call function function_name
      • example: @foo(4 - 1, 2) => 4
  • expression: consists of numbers, operators, function calls, variables

    • example -(a - @bar(1, (2.34 + c) * b)) * 5.1 - d / (100 - 1)
  • operators:

    • unary: +-
    • binary: +-/*
    • parentheses: ()
  • meta command: ;identifier

    • ;mem (show existing variables and functions)
  • instruction:

    • variable assignment (create variable)
    • function declaration (create function)
    • expression (calculate and print value)
    • meta command
  • interpreter: processes instructions

Visit original content creator repository
https://github.com/TuM0xA-S/gocalc

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *