directory

  1. Implementing a programming language with javascript – Preface
  2. Implementing a programming language in javascript – Language vision
  3. Implement a programming language in javascript – write a parser
  4. Use javascript to implement a programming language – character input stream
  5. Use javascript to implement a programming language – lexical analysis
  6. Using javascript to implement a programming language -AST introduction

AST

As mentioned earlier, the parser will build an exact semantically meaningful structure for the program. Our AST node is a plain javascript object that contains a Type attribute, with other attributes varying depending on type.

type AST node
num { type: “num”, value: NUMBER }
str { type: “str”, value: STRING }
bool { type: “bool”, value: true or false }
var { type: “var”, value: NAME }
lambda { type: “lambda”, vars: [ NAME… ] , body: AST }
call { type: “call”, func: AST, args: [ AST… ] }
if { type: “if”, cond: AST, then: AST, else: AST }
assign { type: “assign”, operator: “=”, left: AST, right: AST }
binary { type: “binary”, operator: OPERATOR, left: AST, right: AST }
prog { type: “prog”, prog: [ AST… ] }
let { type: “let”, vars: [ VARS… ] , body: AST }

example

  • Numbers(“num”)
    123.5
    Copy the code
    { type: "num"Value: 123.5}Copy the code
  • Strings(“str”)
    "Hello world"
    Copy the code
    { type: "str", value: "Hello world" }
    Copy the code
  • Booleans(“bool”)
    true
    false
    Copy the code
    { type: "bool", value: true }
    { type: "bool", value: false }
    Copy the code
  • Identifiers(“var”)
    foo
    Copy the code
    { type: "var", value: "foo" }
    Copy the code
  • Functions(“lambda”)
    Lambda (x) 10 λ (x) 10Copy the code
    {
        type: "lambda",
        vars: [ "x" ],
        body: { type: "num" , value: 10}
    }
    Copy the code
  • Functions calls(“call”)
    foo(a, 1)
    Copy the code
    {
        type: "call",
        func: {
            type: "var", value: "foo"
        },
        args: [
            { type: "var", value: "a" },
            { type: "num", value: 1 }
        ]
    }
    Copy the code
  • Conditionals(“if”)
    if foo then bar else baz
    
    -->
    
    {
        type: "if"."cond": { type: "var", value: "foo" },
        "then": { type: "var", value: "bar" },
        "else": { type: "var", value: "baz"}}Copy the code
    if foo then bar
    
    -->
    
    {
        type: "if"."cond": { type: "var", value: "foo" },
        "then": { type: "var", value: "bar"}}Copy the code
  • Assignment(“assign”)
    a = 10
    
    --> 
    
    {
        type: "assign",
        operator: "=",
        left: { type: "var", value: "a" },
        right: { type: "num", value: 10 }
    }
    Copy the code
  • Binary expressions(“binary”)
    z + y * z
    
    -->
    
    {
        type: "binary",
        operator: "+",
        left: { type: "var", value: "x" },
        right: {
            type: "binary",
            operator: "*",
            left: { type: "var", value: "y" },
            right: { type: "var", value: "z"}}}Copy the code
  • Sequences(“prog”)
    {
        a = 5;
        b = a * 2;
        a + b;
    }
    
    --> 
    
    {
        type: "prog",
        prog: [
            {
                type: "assign",
                operator: "=",
                left: { type: "var", value: "a" },
                right: { type: "num", value: 1 }
            },
            {
                "type": "assign"."operator": "="."left": { "type": "var"."value": "b" },
                "right": {
                    "type": "binary"."operator": "*"."left": { "type": "var"."value": "a" },
                    "right": { "type": "num"."value": 2}}}, {"type": "binary"."operator": "+"."left": { "type": "var"."value": "a" },
                "right": { "type": "var"."value": "b"}}}]Copy the code
  • Block scoped variables(“let”)
    let (a = 10, b = a * 10) {
        a + b;
    }
    
    --> 
    
    {
        "type": "let"."vars": [{"name": "a"."def": { "type": "num"."value": 10}}, {"name": "b"."def": {
                    "type": "binary"."operator": "*"."left": { "type": "var"."value": "a" },
                    "right": { "type": "num"."value": 10}}}],"body": {
            "type": "binary"."operator": "+"."left": { "type": "var"."value": "a" },
            "right": { "type": "var"."value": "b"}}}Copy the code

The original link: lisperator.net/pltut/parse…