Python

Capitalism

fork
loading
Files
  • main.py
  • scripts
  • backup
  • manual.txt
  • next.capi
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
import os
from sys import *
import time

tokens = []
interview_modules = []
offered_modules = []
ava_modules = [
	'system',
	'capibase',
	'colorTerm'
]
symbols = {}
functions = {}

class bcolors:
  HEADER = '\033[95m'
  OKBLUE = '\033[94m'
  OKGREEN = '\033[92m'
  WARNING = '\033[93m'
  FAIL = '\033[91m'
  ENDC = '\033[0m'
  BOLD = '\033[1m'
  UNDERLINE = '\033[4m'

def open_file(filename):
	data = open(filename, "r").read()
	data += "<EOF>"
	return data

def lex(filecontents):
	tok = ""
	state = 0
	isexpr = 0
	varstarted = 0
	var = ""
	functionstarted = 0
	functionendstarted = 0
	functioncallstarted = 0
	function = ""
	functionend = ""
	functioncall = ""
	module = ""
	string = ""
	expr = ""
	print(bcolors.ENDC+"")
	print(bcolors.ENDC+"")
	print(bcolors.OKBLUE+bcolors.UNDERLINE+"    ~READING FILE~    ")
	print(bcolors.ENDC+bcolors.UNDERLINE+"|     SUCCESSFUL     |")
	print(bcolors.ENDC+"")
	print(bcolors.ENDC+"")
	print(bcolors.HEADER+"-Capitalism Programming Language (CPL | .capi) is a multi-functional programming language to give the user accessibility to many functions and modules that inherit the feeling and features of other similar ones such as: Lua, CSS, and Javascript.")
	print(bcolors.HEADER+bcolors.UNDERLINE+"-------------------------")
	print(bcolors.ENDC+bcolors.HEADER+"-Owner: David Cogan")
	print(bcolors.HEADER+"-Version: 1.29")
	print(bcolors.ENDC+"")
	print()
	print(bcolors.OKGREEN+">>> running script")
	print(bcolors.OKGREEN+">>> script loaded")
	print(bcolors.OKGREEN+">>> translating")
	print(bcolors.OKGREEN+">>> started lex process")
	print(bcolors.ENDC+bcolors.UNDERLINE+"                               "+bcolors.ENDC)
	print()
	filecontents = list(filecontents)
	for char in filecontents:
		tok += char
		if tok == " ":
			if state == 0:
				tok = ""
			else:
				tok = " "
		elif tok == "\n" or tok == "<EOF>":
			if expr != "" and isexpr == 1:
				tokens.append("EXPR:" + expr)
				expr = ""
			elif expr != "" and isexpr == 0:
				tokens.append("NUM:" + expr)
				expr = ""
			elif var != "":
				tokens.append("VAR:" + var[6:])
				var = ""
				varstarted = 0
			elif functionend != "":
				if functionendstarted == 1:
					tokens.append("END:" + functionend[9:])
					functionendstarted = 0
				functionend = ""
				functionendstarted = 0
			elif functioncall != "":
				if functioncallstarted == 1:
					tokens.append("CALL:" + functioncall[10:])
					functioncallstarted = 0
				functioncall = ""
				functioncallstarted = 0
			tok = ""
		elif tok == "=" and state == 0:
			if expr != "" and isexpr == 0:
				tokens.append("NUM:" + expr)
				expr = ""
			if var != "":
				tokens.append("VAR:" + var[6:])
				var = ""
				varstarted = 0
			elif function != "":
				if functionendstarted == 0 and functioncallstarted == 0:
					tokens.append("FUNCTION:" + function[6:])
				elif functionendstarted == 1:
					tokens.append("END:" + functionend[6:])
					functionendstarted = 0
				else:
					tokens.append("CALL:" + functioncall[10:])
					functioncallstarted = 0
				function = ""
				functionstarted = 0
			if tokens[-1] == "EQUALS":
				tokens[-1] = "EQEQ"
			else:
				tokens.append("EQUALS")
			tok = ""
		elif tok == "house " and state == 0:
			varstarted = 1
			var += tok
			tok = ""
		elif tok == "state " and state == 0:
			functionstarted = 1
			function += tok
			tok = ""
		elif tok == "end " and state == 0:
			functionendstarted = 1
			functionend += tok
			tok = ""
		elif tok == "call " and state == 0:
			functioncallstarted = 1
			functioncall += tok
			tok = ""
		elif varstarted == 1:
			if tok == "<" or tok == ">":
				if var != "":
					tokens.append("VAR:" + var[6:])
					var = ""
					varstarted = 0
			var += tok
			tok = ""
		elif functionstarted == 1:
			if tok == "<" or tok == ">":
				if function != "":
					tokens.append("FUNCTION:" + function[6:])
					function = ""
					functionstarted = 0
			function += tok
			tok = ""
		elif functionendstarted == 1:
			if tok == "<" or tok == ">":
				if functionend != "":
					tokens.append("END:" + functionend[9:])
					functionend = ""
					functionendstarted = 0
			functionend += tok
			tok = ""
		elif functioncallstarted == 1:
			if tok == "<" or tok == ">":
				if functioncall != "":
					tokens.append("CALL:" + functioncall[10:])
					functioncall = ""
					functioncallstarted = 0
			functioncall += tok
			tok = ""
		elif tok == "paycheck":
			tokens.append("PAYCHECK")
			tok = ""
		elif tok == "{tokens}" and state == 0:
			tokens.append("{TOKENS}")
			tok = ""
		elif tok == "if":
			tokens.append("IF")
			tok = ""
		elif tok == "then":
			if expr != "" and isexpr == 0:
				tokens.append("NUM:" + expr)
				expr = ""
			tokens.append("THEN")
			tok = ""
		elif tok == "new":
			tokens.append("NEW")
			tok = ""
		elif tok == "function":
			tokens.append("FUNCTION")
			functionstarted = 0
			tok = ""
		elif tok == "interview":
			tokens.append("INTERVIEW MODULE")
			tok = ""
		elif tok == "offer":
			tokens.append("OFFERED MODULE")
			tok = ""
		elif tok == "tax":
			tokens.append("TAX")
			tok = ""
		#system Module
		elif tok == "system":
			tokens.append("SYSTEM")
			tok = ""
		#continue
		elif tok == "0" or tok == "1" or tok == "2" or tok == "3" or tok == "4" or tok == "5" or tok == "6" or tok == "7" or tok == "8" or tok == "9":
			expr += tok
			tok = ""
		elif tok == "+" or tok == "-" or tok == "/" or tok == "*" or tok == "(" or tok == ")" or tok == "%" or tok == "^":
			isexpr = 1
			expr += tok
			tok = ""
		elif tok == "\t":
			tok = ""
		elif tok == "\"" or tok == " \"":
			if state == 0:
				state = 1
			elif state == 1:
				tokens.append("STRING:" + string + "\"")
				string = ""
				state = 0
				tok = ""
		elif state == 1:
			string += tok
			tok = ""
	#print(symbols)
	#print(tokens)
	#print(modules)
	#return ''
	return tokens

def evalExpression(expr):
	return eval(expr)

def ERROR(errorMsg):
	print(bcolors.ENDC+bcolors.UNDERLINE+"                               "+bcolors.ENDC)
	print()
	print(bcolors.OKGREEN+">>> lexer process completed")
	print(bcolors.OKGREEN+">>> started parse process")
	print(bcolors.WARNING+">>> parse process failed")
	print()
	print(errorMsg)
	quit()

def doPRINT(toPRINT):
	if(toPRINT[0:6] == "STRING"):
		toPRINT = toPRINT[8:]
		toPRINT = toPRINT[:-1]
	elif(toPRINT[0:3] == "NUM"):
		toPRINT = toPRINT[4:]
	elif(toPRINT[0:4] == "EXPR"):
		toPRINT = evalExpression(toPRINT[5:])
	elif(toPRINT[0:3] == "VAR"):
		toPRINT = toPRINT[6:]
	print(toPRINT)

def doASSIGN(varname, varvalue):
	symbols[varname[4:]] = varvalue

def getVARIABLE(varname):
	varname = varname[4:]
	if varname in symbols:
		return symbols[varname]
	else:
		ERROR(bcolors.FAIL+bcolors.UNDERLINE+"VARIABLE ERROR: Undefined Variable"+bcolors.ENDC)
		exit()

def getFUNCTION(varname):
	if varname in functions:
		return functions[varname]
	else:
		ERROR(bcolors.FAIL+bcolors.UNDERLINE+"FUNCTION ERROR: Undefined Variable \""+varname+"\""+bcolors.ENDC)
		exit()

def getINPUT(string, varname):
	string = string[1:]
	string = string[:-1]
	i = input(string+" ")
	symbols[varname] = "STRING:\""+i+"\""

def getMODULE(module, moduleVar):
	if module in moduleVar:
		return True
	else:
		ERROR(bcolors.FAIL+bcolors.UNDERLINE+"MODULE ERROR: Undefined Module \""+module+"\""+bcolors.ENDC)
		exit()

def parse(toks):
	readable = 1
	functionStopped = ""
	functionIn = ""
	i = 0
	ie = 0
	e = 0
	firstEmpty = 1
	while(e < 9):
		tokens.append("EMPTY")
		e+=1
	toks = tokens
	while(i < len(toks)):
		#print(interview_modules)
		if toks[i] + " " + toks[i+1][0:6] == "INTERVIEW MODULE STRING":
			if readable == 1:
				module = toks[i+1]
				module = module[8:]
				module = module[:-1]
				if module in interview_modules:
					ERROR(bcolors.FAIL+bcolors.UNDERLINE+"MODULE ERROR: Already Interviewed \""+module+"\""+bcolors.ENDC)
				else:
					interview_modules.append(module)
			i+=2
		elif toks[i] + " " + toks[i+1][0:6] == "OFFERED MODULE STRING":
			if readable == 1:
				module = toks[i+1]
				module = module[8:]
				module = module[:-1]
				if module in interview_modules:
					if module in offered_modules:
						ERROR(bcolors.FAIL+bcolors.UNDERLINE+"MODULE ERROR: Already Offered \""+module+"\""+bcolors.ENDC)
					else:
						offered_modules.append(module)
				else:
					ERROR(bcolors.FAIL+bcolors.UNDERLINE+"MODULE ERROR: No Interviewed Module \""+module+"\""+bcolors.ENDC)
			i+=2
		elif toks[i] + " " + toks[i+1][0:6] == "PAYCHECK STRING" or toks[i] + " " + toks[i+1][0:3] == "PAYCHECK NUM" or toks[i] + " " + toks[i+1][0:4] == "PAYCHECK EXPR" or toks[i] + " " + toks[i+1][0:3] == "PAYCHECK VAR":
			if readable == 1:
				if toks[i+1][0:6] == "STRING":
					doPRINT(toks[i+1])
				elif toks[i+1][0:3] == "NUM":
					doPRINT(toks[i+1])
				elif toks[i+1][0:4] == "EXPR":
					doPRINT(toks[i+1])
				elif toks[i+1][0:3] == "VAR":
					doPRINT(getVARIABLE(toks[i+1]))
			i+=2
		elif toks[i][0:3] + " " + toks[i+1] + " " + toks[i+2][0:6] == "VAR EQUALS STRING" or toks[i][0:3] + " " + toks[i+1] + " " + toks[i+2][0:3] == "VAR EQUALS NUM" or toks[i][0:3] + " " + toks[i+1] + " " + toks[i+2][0:4] == "VAR EQUALS EXPR" or toks[i][0:3] + " " + toks[i+1] + " " + toks[i+2][0:3] == "VAR EQUALS VAR" or toks[i][:3] + " " + toks[i+1] + " " + toks[i+2][0:3] + " " + toks[i+3][:6] == "VAR EQUALS TAX STRING":
			if readable == 1:
				if toks[i+2][0:6] == "STRING":
					doASSIGN(toks[i],toks[i+2])
				elif toks[i+2][0:3] == "NUM":
					doASSIGN(toks[i],toks[i+2])
				elif toks[i+2][0:4] == "EXPR":
					doASSIGN(toks[i],"NUM:"+str(evalExpression(toks[i+2][5:])))
				elif toks[i+2][0:3] == "VAR":
					doASSIGN(toks[i],getVARIABLE(toks[i+2]))
				elif toks[i+2][0:3] == "TAX":
					getINPUT(toks[i+3][7:],toks[i][4:])
					i+=1
			elif toks[i+2][0:3] == "TAX":
				i+=1 
			i+=3
		elif toks[i][0:8] + " " + toks[i+1] + " " + toks[i+2] + " " + toks[i+4] + " " + toks[i+6] == "FUNCTION EQUALS IF EQEQ THEN" or toks[i][0:8] + " " + toks[i+1] + " " + toks[i+2] + " " + toks[i+3][:3] + " " + toks[i+4] + " " + toks[i+5][:3] + " " + toks[i+5][-4:] == "FUNCTION EQUALS IF VAR EQEQ VAR then":
			if readable == 1:
				if toks[i+3] == toks[i+5]:
					functionStopped = toks[i][9:]
					readable = 1
				elif toks[i+3] != toks[i+5]:
					if toks[i+5][-4:] == "then":
						if symbols[toks[i+3][4:]][8:][:-1] == symbols[toks[i+5][4:][:-4]][8:][:-1]:
							functionStopped = toks[i][9:]
							readable = 1
							i-=1
						elif symbols[toks[i+3][4:]][8:][:-1] != symbols[toks[i+5][4:][:-4]][8:][:-1]:
							functionStopped = toks[i][9:]
							readable = 0
							i-=1
					else:
						functionStopped = toks[i][9:]
						readable = 0
			i+=7
		elif toks[i][0:8] + " " + toks[i+1] + " " + toks[i+2] + " " + toks[i+3] == "FUNCTION EQUALS NEW FUNCTION":
			if readable == 1:
				functionStopped = toks[i][9:]
				functions[toks[i][9:]] = i
				readable = 0
			i+=4
		elif toks[i][0:3] == "END":
			if functionStopped == toks[i][4:] and readable == 0:
				functionStopped = ""
				readable = 1
			elif functionIn == toks[i][4:] and readable == 1:
				i = ie
				ie = 0
				readable = 1
			i+=1
		elif toks[i][0:4] == "CALL":
			if readable == 1:
				ie = i
				functionIn = toks[i][5:]
				i = getFUNCTION(toks[i][5:])
				readable = 1
			i+=4
		elif toks[i] == "SYSTEM" and getMODULE('system', offered_modules) == getMODULE('system', ava_modules) and readable == 1:
			i+=1
			if toks[i] + " " + toks[i+1] == "PAYCHECK {TOKENS}":
				print(bcolors.BOLD+bcolors.UNDERLINE+"tokens:"+bcolors.ENDC+" "+str(tokens))
				i+=2
		elif toks[i] == "EMPTY":
			if firstEmpty == 1:
				print(bcolors.ENDC+bcolors.UNDERLINE+"                               "+bcolors.ENDC)
				print()
				print(bcolors.OKGREEN+">>> lexer process completed")
				print(bcolors.OKGREEN+">>> started parse process")
				print(bcolors.OKGREEN+">>> parse process completed")
				print()
				print(bcolors.WARNING+bcolors.UNDERLINE+"  ~SETTING EMPTY~  "+bcolors.ENDC)
				print(bcolors.ENDC+bcolors.UNDERLINE+"|  SCRIPT CEASED  |")
				print(bcolors.ENDC+bcolors.UNDERLINE+"|    0 ERRORS'    |")
				print(bcolors.ENDC+"")
				firstEmpty = 0
			i = len(toks)
		else:
			#print(tokens)
			print(toks[i])
			ERROR(bcolors.FAIL+bcolors.UNDERLINE+"PARSE ERROR: Unidentified Parsing Condition \""+toks[i]+"\""+bcolors.ENDC)
	#print(tokens)
	#print(interview_modules)
	#print(offered_modules)
	#print(symbols)

#def run():
def run(myFile):
	print(bcolors.OKBLUE+">>> LOADING FILE"+bcolors.ENDC+": "+myFile)
	print(bcolors.OKGREEN+">>> loading complete"+bcolors.ENDC)
	data = open_file(myFile)
	#data = open_file(argv[1])
	toks = lex(data)
	parse(toks)

#run()
#run("test.capi")
os.system("clear")
fileName = input("File Name: ")
os.system("clear")
gotFile = 0
for file in os.listdir("./scripts"):
	if fileName[-5:] == ".capi":
		if fileName == file:
			gotFile = 1
			run(os.path.join("./scripts", fileName))
else:
	if gotFile == 0:
		print(bcolors.OKBLUE+">>> LOADING FILE"+bcolors.ENDC+": ./scripts/"+fileName)
		print(bcolors.WARNING+">>> couldn't load file"+bcolors.ENDC+": ./scripts/"+fileName)
		print()
		print(bcolors.FAIL+bcolors.UNDERLINE+"FILE ERROR: Unidentified File \""+fileName+"\""+bcolors.ENDC)
		exit()