Skip to content

Commit

Permalink
Merge pull request #4 from rohittp0/exponent
Browse files Browse the repository at this point in the history
Exponent
  • Loading branch information
rohittp0 authored Nov 7, 2023
2 parents 6c6456e + 643e86e commit 4bae01f
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 4 deletions.
Binary file added language/__pycache__/constants.cpython-311.pyc
Binary file not shown.
Binary file added language/__pycache__/emitter.cpython-311.pyc
Binary file not shown.
Binary file added language/__pycache__/lexer.cpython-311.pyc
Binary file not shown.
Binary file added language/__pycache__/parser.cpython-311.pyc
Binary file not shown.
1 change: 1 addition & 0 deletions language/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class TokenType(enum.Enum):
GT = 210
GTEQ = 211
MODULO = 212
EXPOD=213
# Brackets.
LPAREN = 301
RPAREN = 302
Expand Down
Binary file not shown.
Binary file not shown.
8 changes: 7 additions & 1 deletion language/lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,13 @@ def parse_operators(self):
elif self.curChar == '-':
return(Token(self.curChar, TokenType.MINUS))
elif self.curChar == '*':
return(Token(self.curChar, TokenType.ASTERISK))
# Check whether this token is * or **
if self.peek() == '*':
lastChar = self.curChar
self.next_char()
return(Token(lastChar + self.curChar, TokenType.EXPOD))
else:
return(Token(self.curChar, TokenType.ASTERISK))
elif self.curChar == '/':
return(Token(self.curChar, TokenType.SLASH))
elif self.curChar == '%':
Expand Down
25 changes: 22 additions & 3 deletions language/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from language.errors.parseing_error import ExpectedTokenError, InvalidTokenError, UndefinedVariableError
from language.lexer import Lexer, Token

expF=False

class Variable:
def __init__(self, name, value):
Expand Down Expand Up @@ -48,8 +49,15 @@ def nl(self, throw=True):
self.next_token()

def primary(self):
global expF
if self.check_token(TokenType.NUMBER):
if self.check_peek(TokenType.EXPOD): # pow( should be present before our number
self.emitter.emit("pow(")
expF=False
self.emitter.emit(self.cur_token.text)
if expF:
self.emitter.emit(")")
expF=False
self.next_token()

elif self.check_token(TokenType.IDENT):
Expand All @@ -65,17 +73,24 @@ def unary(self):
if self.check_token(TokenType.PLUS) or self.check_token(TokenType.MINUS):
self.emitter.emit(self.cur_token.text)
self.next_token()

self.primary()

def term(self):
global expF
if self.check_peek(TokenType.MODULO):
self.emitter.emit("(int)")

self.unary()

while self.check_token(TokenType.ASTERISK) or self.check_token(TokenType.SLASH) or \
self.check_token(TokenType.MODULO):
self.emitter.emit(self.cur_token.text)
self.check_token(TokenType.MODULO) or self.check_token(TokenType.EXPOD):

if self.check_token(TokenType.EXPOD): # , used in parameter seperation of pow function
self.emitter.emit(",")
expF=True
else :
self.emitter.emit(self.cur_token.text)

if self.check_token(TokenType.MODULO):
self.emitter.emit("(int)")
Expand Down Expand Up @@ -127,6 +142,7 @@ def control_statement(self, keyword):
self.emitter.emit_line("}")

def statement(self):
global expF
if self.check_token(TokenType.PRINT):
self.next_token()
self.emitter.emit(f"std::cout")
Expand All @@ -138,7 +154,6 @@ def statement(self):
else:
self.emitter.emit("<<")
self.expression()

self.emitter.emit_line(";")

elif self.check_token(TokenType.IF):
Expand Down Expand Up @@ -182,6 +197,9 @@ def statement(self):
self.match(TokenType.EQ)

self.expression()
if expF:
self.emitter.emit(")")
expF=False
self.emitter.emit_line(";")

elif self.check_token(TokenType.INPUT):
Expand Down Expand Up @@ -209,6 +227,7 @@ def statement(self):

def program(self):
self.emitter.header_line("#include <iostream>")
self.emitter.header_line("#include <cmath>")
self.emitter.header_line("int main(int argc, char *argv[]){")

while self.check_token(TokenType.NEWLINE):
Expand Down
2 changes: 2 additions & 0 deletions test.not
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
PRINT 3*3**3 "\n"
PRINT -1**4

0 comments on commit 4bae01f

Please sign in to comment.