simple-squiggle

A restricted subset of Squiggle
Log | Files | Refs | README

parser.md (1508B)


      1 <!-- Note: This file is automatically generated from source code comments. Changes made in this file will be overridden. -->
      2 
      3 # Function parser
      4 
      5 Create a parser. The function creates a new `math.Parser` object.
      6 
      7 
      8 ## Syntax
      9 
     10 ```js
     11 math.parser()
     12 ```
     13 
     14 ### Parameters
     15 
     16 Parameter | Type | Description
     17 --------- | ---- | -----------
     18 
     19 
     20 ### Returns
     21 
     22 Type | Description
     23 ---- | -----------
     24 Parser | Parser
     25 
     26 
     27 ### Throws
     28 
     29 Type | Description
     30 ---- | -----------
     31 
     32 
     33 ## Examples
     34 
     35 ```js
     36 const parser = new math.parser()
     37 
     38 // evaluate expressions
     39 const a = parser.evaluate('sqrt(3^2 + 4^2)') // 5
     40 const b = parser.evaluate('sqrt(-4)')        // 2i
     41 const c = parser.evaluate('2 inch in cm')    // 5.08 cm
     42 const d = parser.evaluate('cos(45 deg)')     // 0.7071067811865476
     43 
     44 // define variables and functions
     45 parser.evaluate('x = 7 / 2')             // 3.5
     46 parser.evaluate('x + 3')                 // 6.5
     47 parser.evaluate('f(x, y) = x^y')         // f(x, y)
     48 parser.evaluate('f(2, 3)')               // 8
     49 
     50 // get and set variables and functions
     51 const x = parser.get('x')                // 7
     52 const f = parser.get('f')                // function
     53 const g = f(3, 2)                        // 9
     54 parser.set('h', 500)
     55 const i = parser.evaluate('h / 2')       // 250
     56 parser.set('hello', function (name) {
     57   return 'hello, ' + name + '!'
     58 })
     59 parser.evaluate('hello("user")')         // "hello, user!"
     60 
     61 // clear defined functions and variables
     62 parser.clear()
     63 ```
     64 
     65 
     66 ## See also
     67 
     68 [evaluate](evaluate.md),
     69 [compile](compile.md),
     70 [parse](parse.md)