This repository has been archived by the owner on Mar 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
110 lines (90 loc) · 3.64 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import os
import sys
import lexer
import pars
import objgen
import platform
'''
class color:
PURPLE = '\033[95m'
CYAN = '\033[96m'
DARKCYAN = '\033[36m'
BLUE = '\033[94m'
GREEN = '\033[92m'
YELLOW = '\033[93m'
RED = '\033[91m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
END = '\033[0m'
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
'''
def main():
content = "" # This variable will hold the contents of the source code
path = os.getcwd() # Holds path this script was executed from
# Holds the name of the file the user wants to compile
try: fileName = sys.argv[1]
except:
print(' +---------------------------------------------+')
print(' | Y# - v1.0 |')
print(' | Developed By Yehan Wasura |')
print(' +---------------------------------------------+')
print(" Operating System : " + platform.system())
print(" Release : " + platform.release())
print(" Y# Version : v1.2.0")
print(' +---------------------------------------------+')
print("Please use this command for run a program on this compiler : python3 main.py filename.yshp")
return
# Check if the file extension is correct
if fileName[len(fileName) - 5:len(fileName)] != ".yshp":
print("[ERROR] File extension not recognised please make sure extension is '.yshp'")
return # quit programme
# Check to make sure that only one argument is passed
try:
print('[ERROR] Expected 1 argument found 2 (' + sys.argv[1] + ", " + sys.argv[2] + ')')
return # quit programme
except: pass
# Open source code file and get it's content and save it to the 'contents' var
try:
with open(path + "/" + fileName, "r") as file:
content = file.read()
except:
print('Cannot find "' + fileName + '"')
# --------------------------------------
# LEXER
# --------------------------------------
# Create an instance of the lexer class
lex = lexer.Lexer()
# Call lexer method to perform lexical analysis on code
tokens = lex.tokenize(content)
# --------------------------------------
# PARSER
# --------------------------------------
# Create an instance of the parser class
Parser = pars.Parser(tokens)
# Call the parser method and pass in the tokens as arguments
source_ast = Parser.parse(tokens)
# --------------------------------------
# Object Generation
# --------------------------------------
# Create an instance of the Object Generator (objgen) class
object_generator = objgen.ObjectGenerator(source_ast)
# Call the object definer to get python exec() string
exec_string = object_generator.object_definer(False)
# Execute the Y# code that has been transpiled to python code to get output
print(' +---------------------------------------------+')
print(' | Y# - v1.0 |')
print(' | Developed By Yehan Wasura |')
print(' +---------------------------------------------+')
print(" Operating System : " + platform.system())
print(" Release : " + platform.release())
print(" Y# Version : v1.2.0")
print(' +---------------------------------------------+\n\n')
print("Filename: " + fileName +'\n\n')
print('||||||||||||||||||||||| OUTPUT ||||||||||||||||||||||| \n' )
exec(exec_string)
print('\n|||||||||||||||||||||||||||||||||||||||||||||||||||||||| \n')
main()