Skip to content

NoamRothschild/pratt-parser-zig

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

The start of a compiler in zig

NOTE: this project is no longer being worked on.

if this project will ever get continued, all current changes will get reverted and a blank start will follow, since I did too many dumb mistakes (my initial lack of zig knowledge)

What this project is

This project by no means, tries to become a competing - or even a complete compiler ready for production use.

It was created for process of self educating myself with subjects I find intriguing, like pratt-parsing, syntax trees and even getting my first foot out with zig.

What this project can do

  • lexing simple expressions and statements into a meaningful tokens
  • building an AST (just for math expressions as of now)
  • converting an AST of binary expressions into x86 assembly code

example:

for the given input: (8 + 3) * (4 - 1) / 2;:

lexing:

open_args:  (1,0)
number: 8 (1,0)
plus: + (1,0)
number: 3 (1,0)
end_args:  (1,0)
multiply: * (1,0)
open_args:  (1,0)
number: 4 (1,0)
subtract: - (1,0)
number: 1 (1,0)
end_args:  (1,0)
divide: / (1,0)
number: 2 (1,0)
line_terminator:  (1,0)
end_of_file:  (2,0)

ast translation:

scopeStatement {
  expressionStatement {
    binaryExpression {
      left: {
        binaryExpression {
          left: {
            binaryExpression {
              left: {
                numberExpression {
                  value: 8
                }
              },
              operator: {
                type: plus
                value: +
              },
              right: {
                numberExpression {
                  value: 3
                }
              }
            }
          },
          operator: {
            type: multiply
            value: *
          },
          right: {
            binaryExpression {
              left: {
                numberExpression {
                  value: 4
                }
              },
              operator: {
                type: subtract
                value: -
              },
              right: {
                numberExpression {
                  value: 1
                }
              }
            }
          }
        }
      },
      operator: {
        type: divide
        value: /
      },
      right: {
        numberExpression {
          value: 2
        }
      }
    }
  }
}

generated assembly code: (compile with nasm)

  xor eax, eax
  mov eax, 8
  mov ebx, 3
  add eax, ebx
  push eax
  mov ebx, 4
  mov eax, 1
  sub ebx, eax
  pop eax
  imul ebx
  mov ebx, 2
  cdq
  idiv ebx
  ; assert(eax == 15)

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages