@XavierDD/

Nano OS Unif Module

Python

No description

fork
loading
Files
  • main.py
  • readme.txt
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
def programname():
    after_load()
    main__not()
    
import re
import glob
import replit

replit.clear()

int_values = {
     '0' : 0
}
string_values = {}
decimal_values = {
    '0.0' : 0.0,
    'pi' : 3.14
}

def afterload():
    int_values.clear()
    int_values['0'] = 0
    decimal_values.clear()
    decimal_values['0.0'] = 0.000
    string_values.clear()

def create_unif_config():
    config_lines = [
        "# 'auto-debug' is set to false by default.",
        "auto-debug : 'false'",
        "# 'auto-load' is set to null by default. Also, please DO NOT include the '.unif' file extention.",
        "auto-load : null"
    ]
    with open("unif.cfg", 'w+') as config:
        for lines in config_lines:
            config.write(lines)
        config.close()

def error_silencer():
    '''Does nothing, but makes the catch not print.'''
    return -1

def load(file_in, inDebug) :

    print("Opening {} \n".format(file_in))
    line_number = 1
    try :
        with open(file_in, "rb") as data:
            if (data.read(5).lower() == "debug") == True:
                line_number = -1
            for line in data:  
                process(line.decode("utf-8").rstrip(), line_number, inDebug)
                line_number = line_number + 1
    #except TypeError :
        #print("The file, {}, has errors in it.".format(file_in))
    except FileNotFoundError:
        print("The file you tried to open, {}, is non-existent.".format(file_in))
    print("")
    afterload()

def unif_interger_handeler(line, line_number, inDebug):
    '''int i = 0'''
    interger = re.search("int (\S+) = (\S+)", line)
    var_name = interger.group(1)
    var_value = interger.group(2)
    #if int_values.keys().__contains__(var_name) != True:
    int_values[var_name] = int(var_value)
    if inDebug == True :
        print("int {0} = {1}".format(var_name,int(var_value)))

def unif_decimal_handeler(line, line_number, inDebug):
    '''dec d = 1.0'''
    dec = re.search("dec (\S+) = (\S+)", line)
    var_name = dec.group(1)
    var_value = dec.group(2)
    if decimal_values.__contains__(var_name) != True:
        decimal_values[var_name] = float(var_value)
        if inDebug == True :
            print("dec {0} = {1}".format(var_name, float(var_value)))

def unif_number_handler(unif_object):
    if int_values.fromkeys(unif_object):
        return int_values[unif_object]
    elif decimal_values.fromkeys(unif_object):
        return decimal_values[unif_object]
    elif str(unif_object).__contains__('.') != True:
        return int(unif_object)
    else : 
        return float(unif_object)

def unif_math_handler(line, line_number):
    '''
    math (i+i
    math (i*i)
    math (i/i)
    math (i-i)
    ++ i
    -- i
    sqrt ( i )
    abs ( i )
    power ( i , i )
    '''
    matchadd = re.search(str("math ( (\S+) + (\S+) )"), line)
    if matchadd:
        x = matchadd.group(0)
        y = matchadd.group(1)
        if int_values.__contains__(x):
            if decimal_values.__contains__(y) :
                int_values[x] = int(int_values[x] + int(decimal_values[y]))
            if int_values.__contains__(y):
                int_values[x] = int(int_values[x] + int_values[y])

def unif_input_handeler(line, came_from_print, came_from_print_value, line_number):
    unif_input_check = re.search("unif_input",line)
    if unif_input_check:
        return input()
    if came_from_print == True:
        return input(came_from_print_value)

def unif_string_handler(line, line_number, inDebug):
    '''string s = "String"'''
    unif_string = re.search('string (\S+) = "([^"]*)"')
    unif_input_string = re.search("string (\S+) = input", line)
    var_name = unif_string.group(1)
    var_value = unif_string.group(2)
    if unif_string:
        string_values[var_name] = var_value
        if inDebug == True :
            print ("string {0} = {1}".format(var_name, var_value))

def unif_loop_handeler(line, line_number):
    '''
    for (i; i < x; i++)
    stuff
    end
    do 
    ++ i
    while i <10
    '''

def unif_print_handeler(line, line_number):
    '''
    print interger inp
    print string stp
    print decimal dep
    print input (string)
    print "" ''
    '''

    try:
        # Intergers Print Methods
        unif_print_one = re.search("print i (\S+)",line)
        unif_print_two = re.search("print interger (\S+)",line)
        if unif_print_one:
            var_name = unif_print_one.group(1)
            var_value = int_values[var_name]
            print(var_value)
        if unif_print_two: 
            var_name = unif_print_two.group(1)
            var_value = int_values[var_name]
            print(var_value)
        # decimal print stuff
        unif_print_one = re.search("print decimal (\S+)", line)
        unif_print_two = re.search("print d (\S+)", line)
        if unif_print_one:
            var_name = unif_print_one.group(1)
            var_value = decimal_values[var_name]
            print(var_value)
        if unif_print_two:
            var_name = unif_print_two.group(1)
            var_value = decimal_values[var_name]
            print(var_value)
        # string print
        unif_print_one = re.search("print string (\S+)", line)
        unif_print_two = re.search("print s (\S+)", line)
        if unif_print_one:
            var_name = unif_print_one.group(1)
            var_value = string_values[var_name]
            print(var_value)
        if unif_print_two:
            var_name = unif_print_two.group(1)
            var_value = string_values[var_name]
            print (var_value)
        # '' and "" print
        unif_print_one = re.match("print '([^']*)'", line)
        unif_print_two = re.match('print "([^"]*)"', line)
        if unif_print_one:
            print(unif_print_one.group(1))
        if unif_print_two:
            print(unif_print_two.group(1))
        # input string
        #unif_print_one 
        #if unif_print_one:
            print()
        # input '' and ""

    except KeyError:
            print ("Could not find variable on line {}.".format(line_number))
            

def process(line, line_number, inDebug):
    if (line):
        matchmath = re.search("math", line)
        matchprint = re.search("print", line)
        matchinterger = re.search("int (\S+) = (\S+)", line)
        matchdecimal = re.search("dec (\S+) = (\S+)", line)
        matchstring = re.search("string (\S+) = (\S+)", line)
        #matchinput = re.search("unif_input (\S+)", line)
        matchcomment = re.search('\#', line)
        if matchcomment:
            error_silencer()
        else:
            if matchinterger:
                unif_interger_handeler(line, line_number, inDebug)
            if matchstring:
                unif_string_handler(line,line_number, inDebug)
            if matchdecimal:
                unif_decimal_handeler(line, line_number, inDebug)
            #if matchinput:
                #unif_input_handeler(line, True, "", line_number)
            if matchmath:
                unif_math_handler(line,line_number)
            if matchprint:
                unif_print_handeler(line, line_number)

def main__not():
    try :
        with open("unif.cfg") as config_cc:
            for lines in config_cc:
                match_auto_load = re.search("auto-load : (\S+)", lines)
                #What? Why no group?
                match_auto_debug = re.search("auto-debug : '([^']*)'", lines)
                #print(match_auto_debug)
                if match_auto_debug.group(1) == 'true':
                    permiDebug = True
                if match_auto_load and match_auto_load.group(1) != 'null' and match_auto_load.group(1) != 'none':
                    file_to_load_first = match_auto_load.group(1)
                    print("Loading {}.unif from the config.\n".format(file_to_load_first))
                    load(file_to_load_first+'.unif', permiDebug)
    #except AttributeError:
    #    error_silencer()
    except FileNotFoundError:
        create_unif_config()
    except IOError:
        error_silencer()
    first_run = True
    print("Put <INSERT FILE NAME HERE> without the '.unif' file extention.\n")
    while (True) :
        #files_unif = glob.glob("*.unif", recursive=True)
        if (first_run == False):
            if input("Would you like to continue? (y/n)") == 'n':
                break
            print()
        if (False == (first_run)) :
            print("Put the another or the same file name as stated before.")
        #print("List of '.unif' files found. \n(Files my be under sub directories. If so add the path inf front of the files name.\n"+str(files_unif) + "\n")
        file_in = input("File name without '.unif' :") + ".unif"
        try :
            with open(file_in, 'r+') as check_debug:
                inDebug = (check_debug.read(5) == "debug")
                if permiDebug == True:
                    print("\nEntering debug mode.\n") 
                    inDebug = True
                if permiDebug != True and inDebug == True:
                    print("\nEntering debug mode.\n")
                load(file_in ,inDebug)
                check_debug.close()
        except FileNotFoundError:
            error_silencer()
            load(file_in, False)
        first_run = False

class unif_class:
    def __init__(self):
         print()

    def run_unif(self):
         programname()