Giter VIP home page Giter VIP logo

compiler's Introduction

This repository was created as an exercise to learn about compilers and is a work in progress. The compiler compiles math expressions to x86_64 assembly - Intel Syntax.

Build

To build the math compiler and compile a simple math problem. Add the math problem to program.txt then run the build script: build.sh.

./build.sh

# Flags
#
#   -s, --scan-only: the compiler will scan the input file, print each token in 
#                    a human readable format, and exit. assembly is not generated
#                    and ./build_asm.sh will not be executed
#
#   -v, --verbose: the combiler will print log statements to the consol
#

# Example
cat program.txt
4 ^ 2 - 6 ^ 2 + 3

./build.sh
(((4^2)-(6^2))+3)
ASSEMBLY OUTPUT:  -17

The build script has three parts. Part one handles flags/arguments passed to build.sh by the user. Part two compiles the compiler with gcc and executes the binary. The path to program.txt is provided to the compiler as its first command line argument. During the code generation stage of execution the compiler will save the generated assembly to asm/program.asm. The third part calls asm/build_asm.sh which assembles, linkes, and executes the assembly. build_asm.sh uses nasm to assemble and ld to link the generated and prewritten x86_64 assembly. The prewritten assembly provides a built in way to print the result of the generated assembly.

Supported Operators

  • + <- addition
  • - <- subtraction
  • / <- floor division
  • * <- multiplication
  • ^ <- power
  • % <- modulo

Known Limitations

  • No support for floating point numbers in input or output
    • 2.5 + 3 is an invalid expression
    • 5 / 2 will evaluate to 2 instead of 2.5
  • Negative exponents evauate to 0 (related to the lack of floating point support)
    • 4 ^ -3 = 0
  • Limited error detection
    • if there are multiple invalid tokens or multiple syntax errors the compiler will only identify the first one, print the error to the consol, and exit

Example Valid Math Expressions

  • 3 + -2
  • 6 / 3 + 2
  • 6/3+2
  • 4 ^ 2 - 6 ^ 2 + 3
  • (8 + 2) * 834
  • 4-2

Dependencies

In parenthesis are the versions used during development

  • gcc (9.4.0)
  • nasm (2.14.02)
  • ld (2.34)

compiler's People

Contributors

cpsughrue avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.