bin/cli.js
function runStream (input, output, mode, parenthesis) {
  const readline = require('readline')
  const rl = readline.createInterface({
    input: input || process.stdin,
    output: output || process.stdout,
    completer: completer
  })

  if (rl.output.isTTY) {
    rl.setPrompt('> ')
    rl.prompt()
  }

  // load math.js now, right *after* loading the prompt.
  const math = getMath()

  // TODO: automatic insertion of 'ans' before operators like +, -, *, /

  rl.on('line', function (line) {
    const expr = line.trim()

    switch (expr.toLowerCase()) {
      case 'quit':
      case 'exit':
        // exit application
        rl.close()
        break
      case 'clear':
        // clear memory
        scope = {}
        console.log('memory cleared')

        // get next input
        if (rl.output.isTTY) {
          rl.prompt()
        }
        break
      default:
        if (!expr) {
          break
        }
        switch (mode) {
          case 'evaluate':
            // evaluate expression
            try {
              let node = math.parse(expr)
              let res = node.evaluate(scope)

              if (math.isResultSet(res)) {
                // we can have 0 or 1 results in the ResultSet, as the CLI
                // does not allow multiple expressions separated by a return
                res = res.entries[0]
                node = node.blocks
                  .filter(function (entry) { return entry.visible })
                  .map(function (entry) { return entry.node })[0]
              }

              if (node) {
                if (math.isAssignmentNode(node)) {
                  const name = findSymbolName(node)
                  if (name !== null) {
                    scope.ans = scope[name]
                    console.log(name + ' = ' + format(scope[name]))
                  } else {
                    scope.ans = res
                    console.log(format(res))
                  }
                } else if (math.isHelp(res)) {
                  console.log(res.toString())
                } else {
                  scope.ans = res
                  console.log(format(res))
                }
              }
            } catch (err) {
              console.log(err.toString())
            }
            break

          case 'string':
            try {
              const string = math.parse(expr).toString({ parenthesis: parenthesis })
              console.log(string)
            } catch (err) {
              console.log(err.toString())
            }
            break

          case 'tex':
            try {
              const tex = math.parse(expr).toTex({ parenthesis: parenthesis })
              console.log(tex)
            } catch (err) {
              console.log(err.toString())
            }
            break
        }
    }

    // get next input
    if (rl.output.isTTY) {
      rl.prompt()
    }
  })

  rl.on('close', function () {
    console.log()
    process.exit(0)
  })
}
function runStream (input, output, mode, parenthesis) {
  const readline = require('readline')
  const rl = readline.createInterface({
    input: input || process.stdin,
    output: output || process.stdout,
    completer: completer
  })

  if (rl.output.isTTY) {
    rl.setPrompt('> ')
    rl.prompt()
  }

  // load math.js now, right *after* loading the prompt.
  const math = getMath()

  // TODO: automatic insertion of 'ans' before operators like +, -, *, /

  rl.on('line', function (line) {
    const expr = line.trim()

    switch (expr.toLowerCase()) {
      case 'quit':
      case 'exit':
        // exit application
        rl.close()
        break
      case 'clear':
        // clear memory
        scope = {}
        console.log('memory cleared')

        // get next input
        if (rl.output.isTTY) {
          rl.prompt()
        }
        break
      default:
        if (!expr) {
          break
        }
        switch (mode) {
          case 'evaluate':
            // evaluate expression
            try {
              let node = math.parse(expr)
              let res = node.evaluate(scope)

              if (math.isResultSet(res)) {
                // we can have 0 or 1 results in the ResultSet, as the CLI
                // does not allow multiple expressions separated by a return
                res = res.entries[0]
                node = node.blocks
                  .filter(function (entry) { return entry.visible })
                  .map(function (entry) { return entry.node })[0]
              }

              if (node) {
                if (math.isAssignmentNode(node)) {
                  const name = findSymbolName(node)
                  if (name !== null) {
                    scope.ans = scope[name]
                    console.log(name + ' = ' + format(scope[name]))
                  } else {
                    scope.ans = res
                    console.log(format(res))
                  }
                } else if (math.isHelp(res)) {
                  console.log(res.toString())
                } else {
                  scope.ans = res
                  console.log(format(res))
                }
              }
            } catch (err) {
              console.log(err.toString())
            }
            break

          case 'string':
            try {
              const string = math.parse(expr).toString({ parenthesis: parenthesis })
              console.log(string)
            } catch (err) {
              console.log(err.toString())
            }
            break

          case 'tex':
            try {
              const tex = math.parse(expr).toTex({ parenthesis: parenthesis })
              console.log(tex)
            } catch (err) {
              console.log(err.toString())
            }
            break
        }
    }

    // get next input
    if (rl.output.isTTY) {
      rl.prompt()
    }
  })

  rl.on('close', function () {
    console.log()
    process.exit(0)
  })
}
function runStream ( input , output , mode , parenthesis ) { const readline = require ( 'readline' ) const rl = readline . createInterface ( { input : input || process . stdin , output : output || process . stdout , completer : completer } ) if ( rl . output . isTTY ) { rl . setPrompt ( '> ' ) rl . prompt ( ) } // load math.js now, right *after* loading the prompt. const math = getMath ( ) // TODO: automatic insertion of 'ans' before operators like +, -, *, / rl . on ( 'line' , function ( line ) { const expr = line . trim ( ) switch ( expr . toLowerCase ( ) ) { case 'quit' : case 'exit' : // exit application rl . close ( ) break case 'clear' : // clear memory scope = { } console . log ( 'memory cleared' ) // get next input if ( rl . output . isTTY ) { rl . prompt ( ) } break default : if ( ! expr ) { break } switch ( mode ) { case 'evaluate' : // evaluate expression try { let node = math . parse ( expr ) let res = node . evaluate ( scope ) if ( math . isResultSet ( res ) ) { // we can have 0 or 1 results in the ResultSet, as the CLI // does not allow multiple expressions separated by a return res = res . entries [ 0 ] node = node . blocks . filter ( function ( entry ) { return entry . visible } ) . map ( function ( entry ) { return entry . node } ) [ 0 ] } if ( node ) { if ( math . isAssignmentNode ( node ) ) { const name = findSymbolName ( node ) if ( name !== null ) { scope . ans = scope [ name ] console . log ( name + ' = ' + format ( scope [ name ] ) ) } else { scope . ans = res console . log ( format ( res ) ) } } else if ( math . isHelp ( res ) ) { console . log ( res . toString ( ) ) } else { scope . ans = res console . log ( format ( res ) ) } } } catch ( err ) { console . log ( err . toString ( ) ) } break case 'string' : try { const string = math . parse ( expr ) . toString ( { parenthesis : parenthesis } ) console . log ( string ) } catch ( err ) { console . log ( err . toString ( ) ) } break case 'tex' : try { const tex = math . parse ( expr ) . toTex ( { parenthesis : parenthesis } ) console . log ( tex ) } catch ( err ) { console . log ( err . toString ( ) ) } break } } // get next input if ( rl . output . isTTY ) { rl . prompt ( ) } } ) rl . on ( 'close' , function ( ) { console . log ( ) process . exit ( 0 ) } ) }
Run stream, read and evaluate input and stream that to output.
Text lines read from the input are evaluated, and the results are send to
the output.
@param input   Input stream
@param output  Output stream
@param mode    Output mode
@param parenthesis Parenthesis option
Run stream read and evaluate input and stream that to output . Text lines read from the input are evaluated and the results are send to the output .