Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- variables = []
- voids = []
- voidsName = ['int', 'float', 'str', 'input', 'split', 'map', 'count', 'max', 'min', 'nil', 'rev', 'Object']
- operators = ['==', '!=', '>', '<', '>=', '<=', '+', '-', '*', '/', '//', 'or', 'and', '%']
- import sys
- import time
- class ColorPrint:
- @staticmethod
- def print_fail(message, end = '\n'):
- sys.stderr.write('\x1b[1;31m' + message.strip() + '\x1b[0m' + end)
- @staticmethod
- def print_pass(message, end = '\n'):
- sys.stdout.write('\x1b[1;32m' + message.strip() + '\x1b[0m' + end)
- @staticmethod
- def print_warn(message, end = '\n'):
- sys.stderr.write('\x1b[1;33m' + message.strip() + '\x1b[0m' + end)
- @staticmethod
- def print_info(message, end = '\n'):
- sys.stdout.write('\x1b[1;34m' + message.strip() + '\x1b[0m' + end)
- @staticmethod
- def print_bold(message, end = '\n'):
- sys.stdout.write('\x1b[1;37m' + message.strip() + '\x1b[0m' + end)
- def present():
- global variables
- for e in variables:
- ColorPrint.print_pass('name: ' + e.name + '; value: ' + e.value)
- class Variable:
- def __init__(self, name, value, type):
- self.name = name
- self.value = value
- self.type = type
- self.isUsed = False
- self.isGet = False
- class Void:
- def __init__(self, name, code, inputs, line):
- self.name = name
- self.code = "{" + ' '.join(inputs) + "}{" + code + "}"
- self.line = line
- def boolean(val):
- if val:
- return 'true'
- else:
- return 'false'
- def presentVoids():
- global variables
- for e in variables:
- ColorPrint.print_pass('name: ' + e.name + '; value: ' + str(e.value))
- def isIteble(e):
- if str(type(e)).split("'")[1] == 'list':
- return True
- else:
- if dataType(e) == 'str':
- return True
- return False
- def isInt(val):
- if val.count('-') > 0:
- if val[0] == '-':
- if val.count('1') + val.count('2') + val.count('3') + val.count('4') + val.count('5') + val.count('6') + val.count('7') + val.count('8') + val.count('9') + val.count('0') == len(val) - 1:
- return True;
- elif val.count('1') + val.count('2') + val.count('3') + val.count('4') + val.count('5') + val.count('6') + val.count('7') + val.count('8') + val.count('9') + val.count('0') == len(val):
- return True
- return False
- def dataType(val):
- # print('VAL:', val, isInt(val))
- val = str(val)
- if len(list(val)) == 0:
- return None
- if str(type(val)).split("'")[1] == 'str' and list(val)[0] == "\"" and list(val)[-1] == "\"" and val != "\"":
- return 'str'
- if list(val)[0] == "{" and list(val)[-1] == "}" and len(val) > 1:
- return 'void'
- if list(val)[0] == "[" and list(val)[-1] == "]" and len(val) > 1:
- return 'array'
- elif val == 'true' or val == 'false':
- return 'bool'
- else:
- if str(val).count('.') > 0:
- return 'float'
- elif isInt(val):
- return 'int'
- else:
- return None
- def conv(val):
- return val[1:-1]
- def pboolean(val):
- if val == 'true':
- return True
- else:
- return False
- def math(val1, val2, operator):
- type1 = dataType(val1)
- type2 = dataType(val2)
- #print(type1, type2, val1, val2, operator)
- result = 'None'
- if type1 == 'int' and type2 == 'int':
- val1 = int(val1)
- val2 = int(val2)
- if operator == '==':
- result = boolean(val1 == val2)
- if operator == '!=':
- result = boolean(val1 != val2)
- if operator == '>':
- result = boolean(val1 > val2)
- if operator == '<':
- result = boolean(val1 < val2)
- if operator == '>=':
- result = boolean(val1 >= val2)
- if operator == '<=':
- result = boolean(val1 <= val2)
- if operator == '+':
- result = int(val1 + val2)
- if operator == '-':
- result = int(val1 - val2)
- if operator == '*':
- result = int(val1 * val2)
- if operator == '/':
- result = val1 / val2
- if operator == '//':
- result = val1 // val2
- if operator == '%':
- result = val1 % val2
- if type1 == 'float' and type2 == 'float':
- val1 = float(val1)
- val2 = float(val2)
- if operator == '==':
- result = boolean(val1 == val2)
- if operator == '!=':
- result = boolean(val1 != val2)
- if operator == '>':
- result = boolean(val1 > val2)
- if operator == '<':
- result = boolean(val1 < val2)
- if operator == '>=':
- result = boolean(val1 >= val2)
- if operator == '<=':
- result = boolean(val1 <= val2)
- if operator == '+':
- result = val1 + val2
- if operator == '-':
- result = val1 - val2
- if operator == '*':
- result = val1 * val2
- if operator == '/':
- result = val1 / val2
- if type1 == 'str' and type2 == 'str':
- val1 = conv(val1)
- val2 = conv(val2)
- if operator == '==':
- result = boolean(val1 == val2)
- if operator == '!=':
- result = boolean(val1 != val2)
- if operator == '>':
- result = boolean(val1 > val2)
- if operator == '<':
- result = boolean(val1 < val2)
- if operator == '>=':
- result = boolean(val1 >= val2)
- if operator == '<=':
- result = boolean(val1 <= val2)
- if operator == '+':
- result = val1 + val2
- result = "\"" + str(result) + "\""
- if type1 == 'array' and type2 == 'array':
- if operator == '==':
- result = boolean(val1 == val2)
- if operator == '!=':
- result = boolean(val1 != val2)
- if operator == '>':
- result = boolean(len(val1) > len(val2))
- if operator == '<':
- result = boolean(len(val1) < len(val2))
- if operator == '>=':
- result = boolean(len(val1) >= len(val2))
- if operator == '<=':
- result = boolean(len(val1) <= len(val2))
- if operator == '+':
- result = val1 + val2
- if type1 == 'bool' and type2 == 'bool':
- val1 = pboolean(val1)
- val2 = pboolean(val2)
- if operator == '==':
- result = boolean(val1 == val2)
- if operator == '!=':
- result = boolean(val1 != val2)
- if operator == 'or':
- result = boolean(val1 > val2)
- if operator == 'and':
- result = boolean(val1 < val2)
- if operator == '>=':
- result = boolean(val1 >= val2)
- result = result
- if type1 == 'str' and type2 == 'int':
- val1 = conv(val1)
- val2 = int(val2)
- if operator == '*':
- result = val1 * val2
- result = "\"" + str(result) + "\""
- if type1 == 'array' and type2 == 'int':
- val2 = int(val2)
- if operator == '*':
- result = val1 * val2
- #print(result)
- return result
- def findSystemVoid(name):
- return voidsName.count(name) != 0
- def error(val):
- ColorPrint.print_fail(val)
- def findVariableValue(name):
- global variables
- for e in variables:
- if e.name == name:
- variables[variables.index(e)].isUsed = True
- return e.value
- return None
- def findValue(name):
- val = findVariableValue(name)
- if val != None:
- return val
- return None
- def newVariable(name, value, type):
- global variables
- variables += [Variable(name, value, type)]
- def newVoid(name, code, inputs, line):
- global voids
- voids += [Void(name, code, inputs, line)]
- def findVoidValue(name):
- global voids
- for e in voids:
- if e.name == name:
- return e
- if findSystemVoid(name):
- return True
- return None
- def cvbn(x, name):
- return name + "." + x
- def importLibrary(name):
- import os
- global variables, voids
- libs = os.listdir("libraries")
- #print(libs)
- if libs.count(name) != -1:
- variablesLib = variables
- voidsLib = voids
- codeMain = open("libraries/" + name + "/main.ncd").read()
- # print("Main code:", codeMain)
- startCompile(codeMain, 0)
- #print(variables, voids)
- for i in range(len(variables)):
- b = variables[i]
- b.name = name.split(".")[0] + "." + b.name
- variables[i] = b
- for i in range(len(voids)):
- b = voids[i]
- b.name = name.split(".")[0] + "." + b.name
- voids[i] = b
- variables = []
- voids = []
- variables = variablesLib + variables
- voids = voidsLib + voids
- return
- error("Can't find lib " + name.split(".")[0])
- def reloadVariableValue(name, value, type):
- global variables
- for i in range(len(variables)):
- e = variables[i]
- if e.name == name:
- variables[i].value = value
- variables[i].type = type
- variables[i].isUsed = True
- def convv(val):
- return "\"" + str(val) + "\""
- def convertObject(val):
- result = val.split(',')
- resultVar = ""
- for e in result:
- name, value = e.split(':')
- resultVar += name + " " + str(value)
- return resultVar
- def runSystemVoid(name, inputs):
- count = len(inputs)
- print(count)
- if count == 1:
- print(name, inputs)
- if name == 'Object' and dataType(inputs[0]) == 'void':
- return convertObject(conv(inputs[0]))
- if name == 'rev' and isIteble(inputs[0]):
- if dataType(inputs[0]) == 'str':
- return conv(inputs[0])[::-1]
- else:
- return inputs[0][::-1]
- if name == 'nil':
- if dataType(inputs[0]) == 'str':
- inputs[0] = conv(inputs[0])
- return boolean(inputs[0] == 'None')
- if name == 'min' and isIteble(inputs[0]) and dataType(inputs[0]) != 'str':
- res = list(map(dataType, inputs[0]))
- #print(res)
- if res.count('int') == len(res):
- return min(list(map(int, inputs[0])))
- if name == 'max' and isIteble(inputs[0]) and dataType(inputs[0]) != 'str':
- res = list(map(dataType, inputs[0]))
- #print(res)
- if res.count('int') == len(res):
- return max(list(map(int, inputs[0])))
- if name == 'count' and isIteble(inputs[0]):
- return len(inputs[0])
- if name == 'str':
- return "\"" + str(inputs[0]) + "\""
- if name == 'int':
- type2 = dataType(inputs[0])
- if type2 == 'str':
- if dataType(conv(inputs[0])) == 'int':
- return int(conv(inputs[0]))
- if type2 == 'float':
- return int(inputs[0])
- if type2 == 'int':
- return int(inputs[0])
- if name == 'float':
- type2 = dataType(inputs[0])
- if type2 == 'str':
- if dataType(conv(inputs[0])) == 'float':
- return float(inputs[0])
- if type2 == 'int':
- return float(inputs[0])
- if name == 'str':
- return "\"" + inputs[0] + "\""
- if name == 'input':
- return "\"" + input() + "\""
- if count == 2:
- if dataType(inputs[0]) == 'str' and dataType(inputs[1]) == 'str' and name == 'split':
- result = conv(inputs[0]).split()
- return list(map(convv, result))
- if findVoidValue(conv(inputs[0])) != None and str(type(inputs[1])) == "<class 'list'>" and name == "map":
- result = []
- for e in inputs[1]:
- # print(e, inputs)
- result += [runVoids(conv(inputs[0]), [e])]
- return result
- if name == 'input':
- return input()
- def runVoids(name, values):
- print("Functions data:", name, values)
- if findSystemVoid(name):
- return runSystemVoid(name, values)
- code = loadToLine(findVoidValue(name).code)
- inputs = loadToLine(conv(code[0]))
- programm = conv(code[1])
- for i in range(len(inputs)):
- if len(values) > i:
- newVariable(inputs[i], values[i], dataType(values[i]))
- else:
- newVariable(inputs[i], '"None"', 'str')
- output = startCompile(programm, findVoidValue(name).line + 1)
- # print(output)
- return output
- def specialLoad(code, slash):
- asc = [0, 0, 0]
- result = []
- text = ''
- for e in loadToLine(code):
- if e == slash and asc == [0, 0, 0]:
- result += [text]
- text = ''
- continue
- if e == '{':
- asc[0] += 1
- elif e == '}':
- asc[0] -= 1
- elif e == '[':
- asc[1] += 1
- elif e == ']':
- asc[1] -= 1
- elif e == '(':
- asc[2] += 1
- elif e == ')':
- asc[2] -= 1
- text += (e + ' ')
- if text != '':
- result += [text]
- return result
- def getArray(code, slash):
- ret = specialLoad(conv(code), slash)
- result = []
- for e in ret:
- startCompile("###____###____### = " + e, -1)
- output = findVariableValue("###____###____###")
- if dataType(output) == 'int':
- output = int(output)
- if dataType(output) == 'float':
- output = float(output)
- if dataType(output) == 'bool':
- output = boolean(bool(output))
- result += [output]
- variables.pop()
- #print(result)
- return result
- def compileNanoCode(main, line):
- #print(line)
- #presentVoids()
- lastElement = []
- globalElement = []
- value = []
- name = []
- debug = []
- ansScob = 0
- lastOperators = [[]]
- arraySrez = []
- lastArrayName = []
- propusk = 0
- for idx in range(len(main)):
- if propusk != 0:
- propusk -= 1
- continue
- e = main[idx]
- #print(e, value, lastElement, globalElement, lastOperators, name)
- if lastElement == []:
- if e == 'input':
- lastElement = ['input']
- elif e == 'intInput':
- lastElement = ['int_input']
- elif e == "import":
- lastElement = ["import"]
- elif e == 'print':
- lastElement = ['print (']
- globalElement = ['print']
- elif e == 'if':
- value += [['', '']]
- lastElement = ['math first']
- globalElement = ['if']
- elif e == 'for':
- lastElement = ['for name']
- globalElement = ['for']
- elif e == 'while' and dataType(main[-1]) == 'void':
- nameWhile = 'while' + str(int(time.time() * 10 ** 6))
- code = nameWhile + ' = '
- for i in range(1, len(main) - 1):
- code += main[i] + ' '
- compileNanoCode(loadToLine(code), line)
- while str(findVariableValue(nameWhile)) == 'true':
- output = startCompile(conv(e), line + 1)
- if output == 'error':
- return 'error'
- if output != None:
- return output
- compileNanoCode(loadToLine(code), line)
- return
- elif e == 'void':
- lastElement = ['void']
- globalElement = ['void init']
- elif findVoidValue(e) != None:
- lastElement = ['void ( run']
- globalElement = ['void']
- name = [e]
- elif e == 'return':
- value += [['']]
- lastElement = ['math first']
- globalElement = ['return']
- else:
- lastElement = ['variable =']
- globalElement = ['variable']
- name = [e]
- continue
- if lastElement[-1] == "import":
- name += [e]
- globalElement = ["import"]
- lastElement = ["import type"]
- importLibrary(e)
- continue
- if lastElement[-1] == 'void ( run':
- lastElement = ['math first']
- value += [['']]
- continue
- if lastElement[-1] == 'math oper' and globalElement[-1] == 'void' and e == ',':
- lastElement = ['math first']
- value[-1] += ['']
- continue
- print(*value)
- if (lastElement[-1] == 'math oper' or lastElement[-1] == 'math first') and globalElement[-1] == 'void' and e == ')':
- output = runVoids(name[-1], value[-1])
- print("Functions output:", name, value, output)
- value.pop()
- if len(lastOperators[-1]) == 0:
- value[-1][-1] = output
- else:
- #print(value[-1][-1], output, lastOperators)
- value[-1][-1] = math(value[-1][-1], output, lastOperators[-1][-1])
- lastElement = ['math oper']
- globalElement.pop()
- name.pop()
- continue
- if lastElement[-1] == 'void':
- if findVariableValue(e) == None and findVoidValue(e) == None:
- name += [e]
- else:
- error('Line ' + str(line + 1) + ': ' + e + ' already used!')
- return 'error'
- lastElement[-1] = 'void ('
- continue
- if lastElement[-1] == 'void (' and e == '(':
- lastElement[-1] = 'void inputs'
- value += [[]]
- continue
- if lastElement[-1] == 'void inputs next' and e == ')':
- lastElement[-1] = 'void body'
- continue
- if lastElement[-1] == 'void body':
- if dataType(e) == 'void':
- newVoid(name[-1], conv(e), value[-1], line)
- continue
- elif e == 'nil':
- newVoid(name[-1], '', '', line)
- continue
- if lastElement[-1] == 'void inputs' or lastElement[-1] == 'void inputs next':
- value[-1] += [e]
- lastElement[-1] = 'void inputs next'
- continue
- if lastElement[-1] == 'void inputs next' and e == ',':
- lastElement[-1] = 'void inputs'
- continue
- if lastElement[-1] == 'input' :
- if findVariableValue(e) == None:
- newVariable(e, "\"" + input() + "\"", 'str')
- else:
- reloadVariableValue(e, "\"" + input() + "\"", 'str')
- return
- if lastElement[-1] == 'int_input' :
- k = input()
- if dataType(k) != 'int':
- error('Error: Expected Number, found String!')
- return
- if findVariableValue(e) == None:
- newVariable(e, int(k), 'str')
- else:
- reloadVariableValue(e, int(k), 'str')
- return
- if lastElement[-1] == 'print (' and e == '(':
- lastElement[-1] = 'math first'
- value += [['']]
- continue
- if lastElement[-1] == 'for name':
- if e == '_':
- name = ['$for' + str(time()) + '$$']
- else:
- name = [e]
- if findVariableValue(name[-1]) == None:
- newVariable(name[-1], 'None', 'str')
- lastElement = ['for in']
- continue
- if lastElement[-1] == 'for in' and e == 'in':
- lastElement = ['math first']
- value += [['']]
- continue
- if lastElement[-1] == 'math oper' and globalElement[-1] == 'for' and e == '...':
- value[-1] += ['']
- lastElement = ['math first']
- continue
- if e == '(' and (lastElement[-1] == 'math oper' or lastElement[-1] == 'math first'):
- value += [['']]
- lastOperators += [[]]
- lastElement = ['math first']
- ansScob += 1
- continue
- if e == ')' and lastElement[-1] == 'math oper' and ansScob != 0:
- if len(lastOperators[-2]) == 0:
- value[-2][-1] = value[-1][-1]
- else:
- value[-2][-1] = math(value[-2][-1], value[-1][-1], lastOperators[-2][-1])
- lastOperators.pop()
- value.pop()
- ansScob -= 1
- continue
- if lastElement[-1] == 'math oper' and globalElement[-1] == 'for' and e == 'step':
- value[-1] += ['']
- lastElement = ['math first']
- continue
- if lastElement[-1] == 'math oper' and globalElement[-1] == 'for' and dataType(e) == 'void':
- if isIteble(value[-1][-1]):
- iteb = value[-1][-1]
- if dataType(iteb) == 'str':
- iteb = conv(iteb)
- for e1 in iteb:
- if dataType(value[-1][-1]) == 'str':
- e1 = "\"" + e1 + "\""
- reloadVariableValue(name[-1], e1, 'str')
- output = startCompile(conv(e), line + 1)
- if output == 'error':
- return 'error'
- if output != None:
- return output
- else:
- v1 = 0
- v2 = 0
- step = 1
- ans = len(value[-1])
- # print(value)
- if ans == 1:
- v1 = value[-1][-1]
- if ans == 2:
- v1 = value[-1][-2]
- v2 = value[-1][-1]
- if ans == 3:
- v1 = value[-1][-3]
- v2 = value[-1][-2]
- step = value[-1][-1]
- else:
- step = 1
- v2 = int(value[-1][-1])
- for e1 in range(int(v1), int(v2), int(step)):
- reloadVariableValue(name[-1], e1, dataType(e1))
- programm = loadToLines(conv(e))
- output = startCompile(conv(e), line + 1)
- if output == 'error':
- return 'error'
- if output != None:
- return output
- continue
- if lastElement[-1] == 'variable =' and dataType(e) == 'array':
- arraySrez = getArray(e, ':')
- continue
- if lastElement[-1] == 'variable =' and e == '=':
- lastElement[-1] = 'math first'
- value += [['']]
- continue
- if lastElement[-1] == 'math oper' and globalElement[-1] == 'if' and dataType(e) == 'void':
- # print(value)
- if dataType(value[-1][-1]) == 'bool' :
- if value[-1][-1] == 'true':
- output = startCompile(conv(e), line + 1)
- if output == 'error':
- return 'error'
- if output != None:
- return output
- return
- else:
- lastElement[-1] = 'else'
- else:
- error('Error: Сondition of if, must be a boolean variable!')
- return 'error'
- continue
- if lastElement[-1] == 'else' and e == 'else':
- lastElement[-1] = 'else body'
- continue
- if lastElement[-1] == 'else body' and e == 'if':
- lastElement = ['math first']
- globalElement = ['if']
- value += [['', '']]
- continue
- if lastElement[-1] == 'else body' and globalElement[-1] == 'if' and dataType(e) == 'void':
- output = startCompile(conv(e), line + 1)
- if output == 'error':
- return 'error'
- if output != None:
- return output
- return
- if lastElement[-1] == 'math' or lastElement[-1] == 'math first':
- resultVar = findValue(e)
- #print(resultVar, e, globalElement, lastElement, lastOperators, value)
- if findVoidValue(e) != None:
- lastElement[-1] = 'void ( run'
- globalElement += ['void']
- name += [e]
- continue
- if resultVar != None:
- if len(main) - 1 > idx:
- if dataType(main[idx + 1]) == 'array' and dataType(resultVar) == 'array':
- propusk += 1
- code = getArray(main[idx + 1], ':')
- if len(code) == 1:
- resultVar = resultVar[int(code[0])]
- elif len(code) == 2:
- resultVar = resultVar[int(code[0]):int(code[1])]
- elif len(code) == 3:
- resultVar = resultVar[int(code[0]):int(code[1]):int(code[2])]
- if len(value[-1]) == 0 or lastElement[-1] == 'math first':
- if lastElement[-1] == 'math first':
- value[-1][-1] = resultVar
- #print(value)
- else:
- value[-1] = [resultVar]
- else:
- value[-1][-1] = math(value[-1][-1], resultVar, lastOperators[-1][-1])
- lastOperators[-1].pop()
- else:
- if dataType(e) == 'array':
- e = getArray(e, ',')
- if len(value[-1]) == 0 or lastElement[-1] == 'math first':
- if lastElement[-1] == 'math first' and dataType(e) != None:
- value[-1][-1] = e
- else:
- value[-1] = [e]
- else:
- value[-1][-1] = math(value[-1][-1], e, lastOperators[-1][-1])
- lastOperators[-1].pop()
- lastElement[-1] = 'math oper'
- continue
- if e == ')' and globalElement[-1] == 'print':
- if lastElement[-1] == 'array srez':
- if len(lastOperators[-1]) == 0:
- value[-1][-1] = findVariableValue(name[-1])
- else:
- value[-1][-1] = math(value[-1][-1], findVariableValue(name[-1]), lastOperators[-1][-1])
- if dataType(value[-1][-1]) == 'str':
- debug += [conv(value[-1][-1])]
- else:
- debug += [value[-1][-1]]
- print(*debug)
- return
- if e == ',' and lastElement[-1] == 'math oper' and globalElement[-1] == 'print':
- if lastElement[-1] == 'array srez':
- if len(lastOperators[-1]) == 0:
- value[-1][-1] = findVariableValue(name[-1])
- else:
- value[-1][-1] = math(value[-1][-1], findVariableValue(name[-1]), lastOperators[-1][-1])
- if dataType(value[-1][-1]) == 'str':
- debug += [conv(value[-1][-1])]
- else:
- debug += [value[-1][-1]]
- lastElement = ['math first']
- continue
- if lastElement[-1] == 'math oper' and operators.count(e) > 0:
- if operators.count(e) > 0:
- lastOperators[-1] += [e]
- lastElement[-1] = 'math'
- continue
- elif dataType(e) == 'array':
- code = getArray(e, ':')
- if main[main.index(e) - 1] == ")" or len(lastArrayName) == 0:
- if isIteble(value[-1][-1]):
- ex = value[-1][-1]
- else:
- continue
- else:
- ex = findVariableValue(lastArrayName[-1])
- ex.pop()
- resultVar = None
- #print(code)
- if len(code) == 1:
- resultVar = ex[int(code[0])]
- elif len(code) == 2:
- resultVar = ex[int(code[0]):int(code[1])]
- elif len(code) == 3:
- resultVar = ex[int(code[0]):int(code[1]):int(code[2])]
- if len(lastOperators[-1]) == 0:
- if lastElement[-1] == 'math first':
- value[-1][-1] = resultVar
- #print(value)
- else:
- value[-1] = [resultVar]
- else:
- #print(resultVar, e, globalElement, lastElement, lastOperators, value)
- value[-1][-1] = math(value[-1][-1], resultVar, lastOperators[-1][-1])
- lastOperators[-1].pop()
- lastElement[-1] = 'math oper'
- continue
- #print(name, value, lastElement, globalElement)
- if e == '(':
- error("Error: Invalid function '" + name[-1] + "'!")
- else:
- error("Error: Invalid function '" + e + "'!")
- return 'error'
- if len(main) > 1 and len(globalElement) > 0:
- funcId = globalElement[-1]
- if funcId == 'variable':
- if findVariableValue(name[-1]) == None:
- newVariable(name[-1], value[-1][-1], dataType(value[-1][-1]))
- else:
- if arraySrez != [] and isIteble(findVariableValue(name[-1])):
- value = value[-1][-1]
- startValue = findVariableValue(name[-1])
- count = len(arraySrez)
- if count == 1:
- startValue[arraySrez[0]] = value[-1][-1]
- elif count == 2:
- startValue[arraySrez[0]:arraySrez[1]] = value[-1][-1]
- elif count == 3:
- startValue[arraySrez[0]:arraySrez[1]:arraySrez[2]] = value[-1][-1]
- reloadVariableValue(name[-1], startValue, 'list')
- else:
- reloadVariableValue(name[-1], value[-1][-1], dataType(value[-1][-1]))
- if funcId == 'print':
- error("Error: Expected ')'!")
- return 'error'
- if funcId == 'return':
- return value[-1][-1]
- lineEndWithout = ['\t', ' ', '\n']
- lineEndWith = ['(', ')', ',', ':']
- def loadToLines(code):
- result = []
- text = ''
- asc = 0
- asc2 = 0
- for e in code:
- if e == '\n' and asc == 0 and asc2 == 0:
- result += [text]
- text = ''
- continue
- elif e == '{':
- asc += 1
- elif e == '}':
- asc -= 1
- elif e == '[':
- asc2 += 1
- elif e == ']':
- asc2 -= 1
- text += e
- if text != '':
- result += [text]
- return result
- def loadToLine(code):
- result = []
- text = ''
- isString = False
- asc = 0
- asc2 = 0
- for e in code:
- if isString == False and asc == 0 and e != '{' and e != '}'and e != '[' and e != ']' and asc2 == 0:
- if lineEndWithout.count(e) != 0:
- if text != '':
- result += [text]
- text = ''
- continue
- if lineEndWith.count(e) != 0:
- if text != '':
- result += [text]
- text = ''
- result += [e]
- continue
- elif e == '{' and isString == False and asc2 == 0:
- if asc == 0:
- if text != '':
- result += [text]
- text = ''
- asc += 1
- elif e == '}' and isString == False and asc2 == 0:
- if asc == 1:
- result += [text + '}']
- text = ''
- asc -= 1
- continue
- asc -= 1
- elif e == '[' and isString == False and asc == 0:
- if asc2 == 0:
- if text != '':
- result += [text]
- text = ''
- asc2 += 1
- elif e == ']' and isString == False and asc == 0:
- if asc2 == 1:
- result += [text + ']']
- text = ''
- asc2 -= 1
- continue
- asc -= 1
- if e == "\"" and asc == 0 and asc2 == 0:
- if isString:
- result += ['\"' + text + '\"']
- text = ''
- isString = not isString
- continue
- text += e
- if text != '':
- result += [text]
- return result
- programm = open('programm.ncd', 'r').read()
- def startCompile(programm, line):
- codeRET = loadToLines(programm)
- idx = 0
- for e in codeRET:
- pr = loadToLine(str(e))
- output = compileNanoCode(pr, idx + line)
- if output == 'error':
- break;
- if output != None:
- return output
- idx += e.count('\n') + 1
- startCompile(programm, 0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement