Code Monkey home page Code Monkey logo

atlas_compiler's Introduction

Atlas_Compiler

Description

A source-to-source compiler that translates my languages, ATLAS, into C source code. I decided to embark on this project to learn more about: what happens when something is compiled, how C++ and C work, and what it means to design a good programming language.

Logical Components

Consists of:

  • Lexer (Tokenizer)
  • Parser
  • Code Emitter
  • "Helper" classes
    • Variable.h
    • optimizer? (Future)

Formal Syntax

Rules

::= <> consists of
{x} <> one or more of x
[x] <> zero or one of x
() <> just grouping
< x > <> one and only one of x
|| <> means or ( like java)
KW <> denotes that the thing is a keyword
TK(S) <> token that we have already defined
nl <> new line
... <> ellipses means pattern

Structure

program ::= {statement}
statement ::= KW"WRITE" (expression || TK.StringLiteral) TK.Eos
    || KW."STRING" TK.Identifier TK."=" (TK.StringLiteral || TK.Identifier) TK.Eos
    || KW."INT" TK.Identifier TK."=" (TK.IntLiteral || expression) TK.Eos
    || KW."IF" TKS."){" nl {statement} TKS."};" TK.Eos
    || KW."WHILE" TKS."]{" {statement} TKS"};" TK.Eos
    || KW."INPUT" TK.Identifier TK.Eos
    || KW."FOR" TK."[" KW."INT" TK."]" {statment} TK.EOS
    || TK.identifier TK."=" (TK.StringLiteral || TK.IntLiteral) Tk.Eos
comparison ::= expression {(">" || "<" || ">=" || "<=" || "==" || "!=") expression}
expression ::= term {("+" || "-" term)}
term ::= unary {("*" || "/") unary}
unary ::= ["+" || "-"]primary
primary ::= TK.StringLiteral || TK.identifier || TK.FloatLiteral
TK.Eos = ";" End Of Statement -> EOS

Key Words

  • CC - denotes a comment
  • WRITE - standard output function
WRITE "hello world"; CC this will print the given string literal
WRITE a * b + c; CC expressions and variables work as well
  • IF - conditional branch
IF [n >= 5]{
    WRITE "n is not 3";
};
  • ELSE - conditional branch
IF [n == 0]{
        RETURN 1;
    };

ELSE {
    NUM x = n * 10;
    NUM temp = @mystery(x,); CC calls function and saves value to temp
    RETURN  n * temp + 4;
};
  • INPUT - takes input from user
INPUT STRING name; CC INPUT <Data Type> <Variable Name>

WHILE - loop type

CC Print all positive numbers less than a given n
INPUT NUM n;
NUM i = 0;

WHILE [ i < n]{
  WRITE i;
  i = i + 1;
};
  • FOR - loop type
CC you can loop for a given amount of times 
INPUT NUM x;

FOR [x]{
    WRITE "Hello";
};

CC you can also loop for the length of a string

FOR ["Strawberry"]{
    WRITE x;
};
  • DEFINE - used to define a function
CC DEFINE is followed by the function name
CC within the parameters of the function, local 
CC variable names and datatypes are seperated by a colon 
CC all local parameters are ended by a comma 
CC following the dollar sign, we have the return type
DEFINE factorial (n:NUM,) $ NUM {
  CC returns n! factorial

  IF [n == 0]{
    RETURN 1;
  };

  ELSE {
    NUM x = n - 1;
    NUM temp = @factorial(x,);
    RETURN  n * temp;
  };

  RETURN 1;

};
  • RETURN - returns a values within a function
  • NUM - a data type
CC all numbers in ATLAS are assumed to be integers unless otherwise specified
NUM x  = 10;
  • fl - used to specify a float literal
NUM pi = fl3.14;
  • STRING - a data type
STRING name = "Anthony";
  • @ - designates a function call
CC here we have our factorial function again
DEFINE factorial (n:NUM,) $ NUM {
  CC returns n! factorial

  IF [n == 0]{
    RETURN 1;
  };

  ELSE {
    NUM x = n - 1;
    NUM temp = @factorial(x,); CC recursive call
    RETURN  n * temp;
  };

  RETURN 1;

};


WRITE "Please enter a number below";
INPUT NUM in;

NUM answer = @factorial(in,); CC call with variable input

WRITE answer;

atlas_compiler's People

Contributors

aprancl 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.