Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 31.88 KB | None | 0 0
  1.  
  2. variables = []
  3. voids = []
  4.  
  5. voidsName = ['int', 'float', 'str', 'input', 'split', 'map', 'count', 'max', 'min', 'nil', 'rev', 'Object']
  6.  
  7. operators = ['==', '!=', '>', '<', '>=', '<=', '+', '-', '*', '/', '//', 'or', 'and', '%']
  8.  
  9. import sys
  10. import time
  11.  
  12. class ColorPrint:
  13.  
  14.     @staticmethod
  15.     def print_fail(message, end = '\n'):
  16.         sys.stderr.write('\x1b[1;31m' + message.strip() + '\x1b[0m' + end)
  17.  
  18.     @staticmethod
  19.     def print_pass(message, end = '\n'):
  20.         sys.stdout.write('\x1b[1;32m' + message.strip() + '\x1b[0m' + end)
  21.  
  22.     @staticmethod
  23.     def print_warn(message, end = '\n'):
  24.         sys.stderr.write('\x1b[1;33m' + message.strip() + '\x1b[0m' + end)
  25.  
  26.     @staticmethod
  27.     def print_info(message, end = '\n'):
  28.         sys.stdout.write('\x1b[1;34m' + message.strip() + '\x1b[0m' + end)
  29.  
  30.     @staticmethod
  31.     def print_bold(message, end = '\n'):
  32.         sys.stdout.write('\x1b[1;37m' + message.strip() + '\x1b[0m' + end)
  33.  
  34. def present():
  35.     global variables
  36.     for e in variables:
  37.         ColorPrint.print_pass('name: ' + e.name + '; value: ' + e.value)
  38.  
  39. class Variable:
  40.     def __init__(self, name, value, type):
  41.         self.name = name
  42.         self.value = value
  43.         self.type = type
  44.         self.isUsed = False
  45.         self.isGet = False
  46.        
  47. class Void:
  48.     def __init__(self, name, code, inputs, line):
  49.         self.name = name
  50.         self.code = "{" + ' '.join(inputs) + "}{" + code + "}"
  51.         self.line = line
  52.  
  53. def boolean(val):
  54.   if val:
  55.     return 'true'
  56.   else:
  57.     return 'false'
  58.  
  59. def presentVoids():
  60.     global variables
  61.     for e in variables:
  62.         ColorPrint.print_pass('name: ' + e.name + '; value: ' + str(e.value))
  63.  
  64. def isIteble(e):
  65.   if str(type(e)).split("'")[1] == 'list':
  66.     return True
  67.   else:
  68.     if dataType(e) == 'str':
  69.       return True
  70.   return False
  71.  
  72. def isInt(val):
  73.   if val.count('-') > 0:
  74.     if val[0] == '-':
  75.       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:
  76.         return True;
  77.   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):
  78.     return True
  79.   return False
  80.  
  81. def dataType(val):
  82.     # print('VAL:', val, isInt(val))
  83.     val = str(val)
  84.     if len(list(val)) == 0:
  85.       return None
  86.     if str(type(val)).split("'")[1] == 'str' and list(val)[0] == "\"" and list(val)[-1] == "\"" and val != "\"":
  87.         return 'str'
  88.     if list(val)[0] == "{" and list(val)[-1] == "}" and len(val) > 1:
  89.         return 'void'
  90.     if list(val)[0] == "[" and list(val)[-1] == "]" and len(val) > 1:
  91.         return 'array'
  92.     elif val == 'true' or val == 'false':
  93.         return 'bool'
  94.     else:
  95.         if str(val).count('.') > 0:
  96.             return 'float'
  97.         elif isInt(val):
  98.             return 'int'
  99.         else:
  100.           return None
  101.  
  102.  
  103. def conv(val):
  104.     return val[1:-1]
  105.    
  106. def pboolean(val):
  107.     if val == 'true':
  108.         return True
  109.     else:
  110.         return False
  111.  
  112.  
  113. def math(val1, val2, operator):
  114.     type1 = dataType(val1)
  115.     type2 = dataType(val2)
  116.     #print(type1, type2, val1, val2, operator)
  117.     result = 'None'
  118.     if type1 == 'int' and type2 == 'int':
  119.         val1 = int(val1)
  120.         val2 = int(val2)
  121.         if operator == '==':
  122.             result = boolean(val1 == val2)
  123.         if operator == '!=':
  124.             result = boolean(val1 != val2)
  125.         if operator == '>':
  126.             result = boolean(val1 > val2)
  127.         if operator == '<':
  128.             result = boolean(val1 < val2)
  129.         if operator == '>=':
  130.             result = boolean(val1 >= val2)
  131.         if operator == '<=':
  132.             result = boolean(val1 <= val2)
  133.         if operator == '+':
  134.             result = int(val1 + val2)
  135.         if operator == '-':
  136.             result = int(val1 - val2)
  137.         if operator == '*':
  138.             result = int(val1 * val2)
  139.         if operator == '/':
  140.             result = val1 / val2
  141.         if operator == '//':
  142.             result = val1 // val2
  143.         if operator == '%':
  144.             result = val1 % val2
  145.     if type1 == 'float' and type2 == 'float':
  146.         val1 = float(val1)
  147.         val2 = float(val2)
  148.         if operator == '==':
  149.             result = boolean(val1 == val2)
  150.         if operator == '!=':
  151.             result = boolean(val1 != val2)
  152.         if operator == '>':
  153.             result = boolean(val1 > val2)
  154.         if operator == '<':
  155.             result = boolean(val1 < val2)
  156.         if operator == '>=':
  157.             result = boolean(val1 >= val2)
  158.         if operator == '<=':
  159.             result = boolean(val1 <= val2)
  160.         if operator == '+':
  161.             result = val1 + val2
  162.         if operator == '-':
  163.             result = val1 - val2
  164.         if operator == '*':
  165.             result = val1 * val2
  166.         if operator == '/':
  167.             result = val1 / val2
  168.     if type1 == 'str' and type2 == 'str':
  169.         val1 = conv(val1)
  170.         val2 = conv(val2)
  171.         if operator == '==':
  172.             result = boolean(val1 == val2)
  173.         if operator == '!=':
  174.             result = boolean(val1 != val2)
  175.         if operator == '>':
  176.             result = boolean(val1 > val2)
  177.         if operator == '<':
  178.             result = boolean(val1 < val2)
  179.         if operator == '>=':
  180.             result = boolean(val1 >= val2)
  181.         if operator == '<=':
  182.             result = boolean(val1 <= val2)
  183.         if operator == '+':
  184.             result = val1 + val2
  185.             result = "\"" + str(result) + "\""
  186.     if type1 == 'array' and type2 == 'array':
  187.         if operator == '==':
  188.             result = boolean(val1 == val2)
  189.         if operator == '!=':
  190.             result = boolean(val1 != val2)
  191.         if operator == '>':
  192.             result = boolean(len(val1) > len(val2))
  193.         if operator == '<':
  194.             result = boolean(len(val1) < len(val2))
  195.         if operator == '>=':
  196.             result = boolean(len(val1) >= len(val2))
  197.         if operator == '<=':
  198.             result = boolean(len(val1) <= len(val2))
  199.         if operator == '+':
  200.             result = val1 + val2
  201.     if type1 == 'bool' and type2 == 'bool':
  202.         val1 = pboolean(val1)
  203.         val2 = pboolean(val2)
  204.         if operator == '==':
  205.             result = boolean(val1 == val2)
  206.         if operator == '!=':
  207.             result = boolean(val1 != val2)
  208.         if operator == 'or':
  209.             result = boolean(val1 > val2)
  210.         if operator == 'and':
  211.             result = boolean(val1 < val2)
  212.         if operator == '>=':
  213.             result = boolean(val1 >= val2)
  214.         result = result
  215.     if type1 == 'str' and type2 == 'int':
  216.         val1 = conv(val1)
  217.         val2 = int(val2)
  218.         if operator == '*':
  219.             result = val1 * val2
  220.         result = "\"" + str(result) + "\""
  221.     if type1 == 'array' and type2 == 'int':
  222.         val2 = int(val2)
  223.         if operator == '*':
  224.             result = val1 * val2
  225.     #print(result)
  226.     return result
  227.  
  228. def findSystemVoid(name):
  229.   return voidsName.count(name) != 0
  230.  
  231. def error(val):
  232.   ColorPrint.print_fail(val)
  233.  
  234. def findVariableValue(name):
  235.     global variables
  236.     for e in variables:
  237.         if e.name == name:
  238.             variables[variables.index(e)].isUsed = True
  239.             return e.value
  240.     return None
  241.  
  242. def findValue(name):
  243.     val = findVariableValue(name)
  244.     if val != None:
  245.         return val
  246.     return None
  247.  
  248.  
  249. def newVariable(name, value, type):
  250.     global variables
  251.     variables += [Variable(name, value, type)]
  252.    
  253. def newVoid(name, code, inputs, line):
  254.     global voids
  255.     voids += [Void(name, code, inputs, line)]
  256.  
  257. def findVoidValue(name):
  258.     global voids
  259.     for e in voids:
  260.         if e.name == name:
  261.             return e
  262.     if findSystemVoid(name):
  263.       return True
  264.     return None
  265.  
  266. def cvbn(x, name):
  267.   return name + "." + x
  268.  
  269. def importLibrary(name):
  270.   import os
  271.   global variables, voids
  272.   libs = os.listdir("libraries")
  273.   #print(libs)
  274.   if libs.count(name) != -1:
  275.     variablesLib = variables
  276.     voidsLib = voids
  277.     codeMain = open("libraries/" + name + "/main.ncd").read()
  278.     # print("Main code:", codeMain)
  279.     startCompile(codeMain, 0)
  280.     #print(variables, voids)
  281.     for i in range(len(variables)):
  282.       b = variables[i]
  283.       b.name = name.split(".")[0] + "." + b.name
  284.       variables[i] = b
  285.     for i in range(len(voids)):
  286.       b = voids[i]
  287.       b.name = name.split(".")[0] + "." + b.name
  288.       voids[i] = b
  289.     variables = []
  290.     voids = []
  291.     variables = variablesLib + variables
  292.     voids = voidsLib + voids
  293.     return
  294.   error("Can't find lib " + name.split(".")[0])
  295.  
  296.  
  297. def reloadVariableValue(name, value, type):
  298.     global variables
  299.     for i in range(len(variables)):
  300.         e = variables[i]
  301.         if e.name == name:
  302.             variables[i].value = value
  303.             variables[i].type = type
  304.             variables[i].isUsed = True
  305.  
  306. def convv(val):
  307.   return "\"" + str(val) + "\""
  308.  
  309. def convertObject(val):
  310.   result = val.split(',')
  311.   resultVar = ""
  312.   for e in result:
  313.     name, value = e.split(':')
  314.     resultVar += name + " " + str(value)
  315.   return resultVar
  316.  
  317.  
  318. def runSystemVoid(name, inputs):
  319.   count = len(inputs)
  320.   print(count)
  321.   if count == 1:
  322.     print(name, inputs)
  323.     if name == 'Object' and dataType(inputs[0]) == 'void':
  324.       return convertObject(conv(inputs[0]))
  325.     if name == 'rev' and isIteble(inputs[0]):
  326.       if dataType(inputs[0]) == 'str':
  327.         return conv(inputs[0])[::-1]
  328.       else:
  329.         return inputs[0][::-1]
  330.     if name == 'nil':
  331.       if dataType(inputs[0]) == 'str':
  332.         inputs[0] = conv(inputs[0])
  333.       return boolean(inputs[0] == 'None')
  334.     if name == 'min' and isIteble(inputs[0]) and dataType(inputs[0]) != 'str':
  335.       res = list(map(dataType, inputs[0]))
  336.       #print(res)
  337.       if res.count('int') == len(res):
  338.         return min(list(map(int, inputs[0])))
  339.     if name == 'max' and isIteble(inputs[0]) and dataType(inputs[0]) != 'str':
  340.       res = list(map(dataType, inputs[0]))
  341.       #print(res)
  342.       if res.count('int') == len(res):
  343.         return max(list(map(int, inputs[0])))
  344.     if name == 'count' and isIteble(inputs[0]):
  345.       return len(inputs[0])
  346.     if name == 'str':
  347.       return "\"" + str(inputs[0]) + "\""
  348.     if name == 'int':
  349.       type2 = dataType(inputs[0])
  350.       if type2 == 'str':
  351.         if dataType(conv(inputs[0])) == 'int':
  352.           return int(conv(inputs[0]))
  353.       if type2 == 'float':
  354.         return int(inputs[0])
  355.       if type2 == 'int':
  356.           return int(inputs[0])
  357.     if name == 'float':
  358.       type2 = dataType(inputs[0])
  359.       if type2 == 'str':
  360.         if dataType(conv(inputs[0])) == 'float':
  361.           return float(inputs[0])
  362.       if type2 == 'int':
  363.         return float(inputs[0])
  364.     if name == 'str':
  365.       return "\"" + inputs[0] + "\""
  366.     if name == 'input':
  367.       return "\"" + input() + "\""
  368.   if count == 2:
  369.     if dataType(inputs[0]) == 'str' and dataType(inputs[1]) == 'str' and name == 'split':
  370.       result = conv(inputs[0]).split()
  371.       return list(map(convv, result))
  372.     if findVoidValue(conv(inputs[0])) != None and str(type(inputs[1])) == "<class 'list'>" and name == "map":
  373.       result = []
  374.       for e in inputs[1]:
  375.         # print(e, inputs)
  376.         result += [runVoids(conv(inputs[0]), [e])]
  377.       return result
  378.   if name == 'input':
  379.     return input()
  380.  
  381.            
  382. def runVoids(name, values):
  383.     print("Functions data:", name, values)
  384.     if findSystemVoid(name):
  385.       return runSystemVoid(name, values)
  386.     code = loadToLine(findVoidValue(name).code)
  387.     inputs = loadToLine(conv(code[0]))
  388.     programm = conv(code[1])
  389.     for i in range(len(inputs)):
  390.         if len(values) > i:
  391.             newVariable(inputs[i], values[i], dataType(values[i]))
  392.         else:
  393.             newVariable(inputs[i], '"None"', 'str')
  394.     output = startCompile(programm, findVoidValue(name).line + 1)
  395.     # print(output)
  396.     return output
  397.  
  398. def specialLoad(code, slash):
  399.   asc = [0, 0, 0]
  400.   result = []
  401.   text = ''
  402.   for e in loadToLine(code):
  403.     if e == slash and asc == [0, 0, 0]:
  404.       result += [text]
  405.       text = ''
  406.       continue
  407.     if e == '{':
  408.       asc[0] += 1
  409.     elif e == '}':
  410.       asc[0] -= 1
  411.     elif e == '[':
  412.       asc[1] += 1
  413.     elif e == ']':
  414.       asc[1] -= 1
  415.     elif e == '(':
  416.       asc[2] += 1
  417.     elif e == ')':
  418.       asc[2] -= 1
  419.     text += (e + ' ')
  420.   if text != '':
  421.     result += [text]
  422.   return result
  423.  
  424. def getArray(code, slash):
  425.   ret = specialLoad(conv(code), slash)
  426.   result = []
  427.   for e in ret:
  428.     startCompile("###____###____### = " + e, -1)
  429.     output = findVariableValue("###____###____###")
  430.     if dataType(output) == 'int':
  431.       output = int(output)
  432.     if dataType(output) == 'float':
  433.       output = float(output)
  434.     if dataType(output) == 'bool':
  435.       output = boolean(bool(output))
  436.     result += [output]
  437.     variables.pop()
  438.   #print(result)
  439.   return result
  440.    
  441.  
  442. def compileNanoCode(main, line):
  443.     #print(line)
  444.     #presentVoids()
  445.     lastElement = []
  446.     globalElement = []
  447.     value = []
  448.     name = []
  449.     debug = []
  450.     ansScob = 0
  451.     lastOperators = [[]]
  452.     arraySrez = []
  453.     lastArrayName = []
  454.     propusk = 0
  455.     for idx in range(len(main)):
  456.         if propusk != 0:
  457.           propusk -= 1
  458.           continue
  459.         e = main[idx]
  460.         #print(e, value, lastElement, globalElement, lastOperators, name)
  461.         if lastElement == []:
  462.             if e == 'input':
  463.                 lastElement = ['input']
  464.             elif e == 'intInput':
  465.                 lastElement = ['int_input']
  466.             elif e == "import":
  467.                 lastElement = ["import"]
  468.             elif e == 'print':
  469.                 lastElement = ['print (']
  470.                 globalElement = ['print']
  471.             elif e == 'if':
  472.                 value += [['', '']]
  473.                 lastElement = ['math first']
  474.                 globalElement = ['if']
  475.             elif e == 'for':
  476.                 lastElement = ['for name']
  477.                 globalElement = ['for']
  478.             elif e == 'while' and dataType(main[-1]) == 'void':
  479.               nameWhile = 'while' + str(int(time.time() * 10 ** 6))
  480.               code = nameWhile + ' = '
  481.               for i in range(1, len(main) - 1):
  482.                 code += main[i] + ' '
  483.               compileNanoCode(loadToLine(code), line)
  484.               while str(findVariableValue(nameWhile)) == 'true':
  485.                 output = startCompile(conv(e), line + 1)
  486.                 if output == 'error':
  487.                   return 'error'
  488.                 if output != None:
  489.                   return output
  490.                 compileNanoCode(loadToLine(code), line)
  491.               return
  492.             elif e == 'void':
  493.                 lastElement = ['void']
  494.                 globalElement = ['void init']
  495.             elif findVoidValue(e) != None:
  496.                 lastElement = ['void ( run']
  497.                 globalElement = ['void']
  498.                 name = [e]
  499.             elif e == 'return':
  500.                 value += [['']]
  501.                 lastElement = ['math first']
  502.                 globalElement = ['return']
  503.             else:
  504.                 lastElement = ['variable =']
  505.                 globalElement = ['variable']
  506.                 name = [e]
  507.             continue
  508.  
  509.         if lastElement[-1] == "import":
  510.             name += [e]
  511.             globalElement = ["import"]
  512.             lastElement = ["import type"]
  513.             importLibrary(e)
  514.             continue
  515.        
  516.         if lastElement[-1] == 'void ( run':
  517.             lastElement = ['math first']
  518.             value += [['']]
  519.             continue
  520.        
  521.         if lastElement[-1] == 'math oper' and globalElement[-1] == 'void' and e == ',':
  522.             lastElement = ['math first']
  523.             value[-1] += ['']
  524.             continue
  525.  
  526.         print(*value)
  527.        
  528.         if (lastElement[-1] == 'math oper' or lastElement[-1] == 'math first') and globalElement[-1] == 'void' and e == ')':
  529.             output = runVoids(name[-1], value[-1])
  530.             print("Functions output:", name, value, output)
  531.             value.pop()
  532.             if len(lastOperators[-1]) == 0:
  533.                 value[-1][-1] = output
  534.             else:
  535.                 #print(value[-1][-1], output, lastOperators)
  536.                 value[-1][-1] = math(value[-1][-1], output, lastOperators[-1][-1])
  537.             lastElement = ['math oper']
  538.             globalElement.pop()
  539.             name.pop()
  540.             continue
  541.        
  542.         if lastElement[-1] == 'void':
  543.             if findVariableValue(e) == None and findVoidValue(e) == None:
  544.                 name += [e]
  545.             else:
  546.                 error('Line ' + str(line + 1) + ': ' + e + ' already used!')
  547.                 return 'error'
  548.             lastElement[-1] = 'void ('
  549.             continue
  550.        
  551.         if lastElement[-1] == 'void (' and e == '(':
  552.             lastElement[-1] = 'void inputs'
  553.             value += [[]]
  554.             continue
  555.        
  556.         if lastElement[-1] == 'void inputs next' and e == ')':
  557.             lastElement[-1] = 'void body'
  558.             continue
  559.        
  560.         if lastElement[-1] == 'void body':
  561.             if dataType(e) == 'void':
  562.                 newVoid(name[-1], conv(e), value[-1], line)
  563.                 continue
  564.             elif e == 'nil':
  565.                 newVoid(name[-1], '', '', line)
  566.                 continue
  567.                
  568.        
  569.         if lastElement[-1] == 'void inputs' or lastElement[-1] == 'void inputs next':
  570.             value[-1] += [e]
  571.             lastElement[-1] = 'void inputs next'
  572.             continue
  573.        
  574.         if lastElement[-1] == 'void inputs next' and e == ',':
  575.             lastElement[-1] = 'void inputs'
  576.             continue
  577.  
  578.         if lastElement[-1] == 'input' :
  579.             if findVariableValue(e) == None:
  580.                 newVariable(e, "\"" + input() + "\"", 'str')
  581.             else:
  582.                 reloadVariableValue(e, "\"" + input() + "\"", 'str')
  583.             return
  584.        
  585.        
  586.  
  587.         if lastElement[-1] == 'int_input' :
  588.             k = input()
  589.             if dataType(k) != 'int':
  590.                 error('Error: Expected Number, found String!')
  591.                 return
  592.             if findVariableValue(e) == None:
  593.                 newVariable(e, int(k), 'str')
  594.             else:
  595.                 reloadVariableValue(e, int(k), 'str')
  596.             return
  597.  
  598.         if lastElement[-1] == 'print (' and e == '(':
  599.             lastElement[-1] = 'math first'
  600.             value += [['']]
  601.             continue
  602.  
  603.         if lastElement[-1] == 'for name':
  604.             if e == '_':
  605.               name = ['$for' + str(time()) + '$$']
  606.             else:
  607.               name = [e]
  608.             if findVariableValue(name[-1]) == None:
  609.               newVariable(name[-1], 'None', 'str')
  610.             lastElement = ['for in']
  611.             continue
  612.  
  613.         if lastElement[-1] == 'for in' and e == 'in':
  614.             lastElement = ['math first']
  615.             value += [['']]
  616.             continue
  617.  
  618.         if lastElement[-1] == 'math oper' and globalElement[-1] == 'for' and e == '...':
  619.             value[-1] += ['']
  620.             lastElement = ['math first']
  621.             continue
  622.  
  623.         if e == '(' and (lastElement[-1] == 'math oper' or lastElement[-1] == 'math first'):
  624.             value += [['']]
  625.             lastOperators += [[]]
  626.             lastElement = ['math first']
  627.             ansScob += 1
  628.             continue
  629.  
  630.         if e == ')' and lastElement[-1] == 'math oper' and ansScob != 0:
  631.             if len(lastOperators[-2]) == 0:
  632.               value[-2][-1] = value[-1][-1]
  633.             else:
  634.               value[-2][-1] = math(value[-2][-1], value[-1][-1], lastOperators[-2][-1])
  635.             lastOperators.pop()
  636.             value.pop()
  637.             ansScob -= 1
  638.             continue
  639.  
  640.         if lastElement[-1] == 'math oper' and globalElement[-1] == 'for' and e == 'step':
  641.             value[-1] += ['']
  642.             lastElement = ['math first']
  643.             continue
  644.        
  645.         if lastElement[-1] == 'math oper' and globalElement[-1] == 'for' and dataType(e) == 'void':
  646.             if isIteble(value[-1][-1]):
  647.                 iteb =  value[-1][-1]
  648.                 if dataType(iteb) == 'str':
  649.                   iteb = conv(iteb)
  650.                 for e1 in iteb:
  651.                     if dataType(value[-1][-1]) == 'str':
  652.                       e1 = "\"" + e1 + "\""
  653.                     reloadVariableValue(name[-1], e1, 'str')
  654.                     output = startCompile(conv(e), line + 1)
  655.                     if output == 'error':
  656.                       return 'error'
  657.                     if output != None:
  658.                       return output
  659.             else:
  660.                 v1 = 0
  661.                 v2 = 0
  662.                 step = 1
  663.                 ans = len(value[-1])
  664.                 # print(value)
  665.                 if ans == 1:
  666.                     v1 = value[-1][-1]
  667.                 if ans == 2:
  668.                     v1 = value[-1][-2]
  669.                     v2 = value[-1][-1]
  670.                 if ans == 3:
  671.                     v1 = value[-1][-3]
  672.                     v2 = value[-1][-2]
  673.                     step = value[-1][-1]
  674.                 else:
  675.                     step = 1
  676.                     v2 = int(value[-1][-1])
  677.                
  678.                 for e1 in range(int(v1), int(v2), int(step)):
  679.                     reloadVariableValue(name[-1], e1, dataType(e1))
  680.                     programm = loadToLines(conv(e))
  681.                     output = startCompile(conv(e), line + 1)
  682.                     if output == 'error':
  683.                       return 'error'
  684.                     if output != None:
  685.                       return output
  686.                  
  687.             continue
  688.  
  689.         if lastElement[-1] == 'variable =' and dataType(e) == 'array':
  690.             arraySrez = getArray(e, ':')
  691.             continue
  692.  
  693.         if lastElement[-1] == 'variable =' and e == '=':
  694.             lastElement[-1] = 'math first'
  695.             value += [['']]
  696.             continue
  697.  
  698.         if lastElement[-1] == 'math oper' and globalElement[-1] == 'if' and dataType(e) == 'void':
  699.             # print(value)
  700.             if dataType(value[-1][-1]) == 'bool' :
  701.               if value[-1][-1] == 'true':
  702.                 output = startCompile(conv(e), line + 1)
  703.                 if output == 'error':
  704.                   return 'error'
  705.                 if output != None:
  706.                   return output
  707.                 return
  708.               else:
  709.                 lastElement[-1] = 'else'
  710.             else:
  711.               error('Error: Сondition of if, must be a boolean variable!')
  712.               return 'error'
  713.             continue
  714.  
  715.         if lastElement[-1] == 'else' and e == 'else':
  716.             lastElement[-1] = 'else body'
  717.             continue
  718.  
  719.         if lastElement[-1] == 'else body' and e == 'if':
  720.             lastElement = ['math first']
  721.             globalElement = ['if']
  722.             value += [['', '']]
  723.             continue
  724.  
  725.         if lastElement[-1] == 'else body' and globalElement[-1] == 'if' and dataType(e) == 'void':
  726.             output = startCompile(conv(e), line + 1)
  727.             if output == 'error':
  728.               return 'error'
  729.             if output != None:
  730.               return output
  731.             return
  732.              
  733.         if lastElement[-1] == 'math' or lastElement[-1] == 'math first':
  734.             resultVar = findValue(e)
  735.             #print(resultVar, e, globalElement, lastElement, lastOperators, value)
  736.             if findVoidValue(e) != None:
  737.                 lastElement[-1] = 'void ( run'
  738.                 globalElement += ['void']
  739.                 name += [e]
  740.                 continue
  741.             if resultVar != None:
  742.  
  743.                 if len(main) - 1 > idx:
  744.                   if dataType(main[idx + 1]) == 'array' and dataType(resultVar) == 'array':
  745.                     propusk += 1
  746.                     code = getArray(main[idx + 1], ':')
  747.                     if len(code) == 1:
  748.                       resultVar = resultVar[int(code[0])]
  749.                     elif len(code) == 2:
  750.                       resultVar = resultVar[int(code[0]):int(code[1])]
  751.                     elif len(code) == 3:
  752.                       resultVar = resultVar[int(code[0]):int(code[1]):int(code[2])]
  753.  
  754.                 if len(value[-1]) == 0 or lastElement[-1] == 'math first':
  755.                     if lastElement[-1] == 'math first':
  756.                       value[-1][-1] = resultVar
  757.                       #print(value)
  758.                     else:
  759.                       value[-1] = [resultVar]
  760.                 else:
  761.                     value[-1][-1] = math(value[-1][-1], resultVar, lastOperators[-1][-1])
  762.                     lastOperators[-1].pop()
  763.             else:
  764.                 if dataType(e) == 'array':
  765.                   e = getArray(e, ',')
  766.  
  767.                 if len(value[-1]) == 0 or lastElement[-1] == 'math first':
  768.                     if lastElement[-1] == 'math first' and dataType(e) != None:
  769.                       value[-1][-1] = e
  770.                     else:
  771.                       value[-1] = [e]
  772.                 else:
  773.                     value[-1][-1] = math(value[-1][-1], e, lastOperators[-1][-1])
  774.                     lastOperators[-1].pop()
  775.             lastElement[-1] = 'math oper'
  776.             continue
  777.  
  778.         if e == ')' and globalElement[-1] == 'print':
  779.           if lastElement[-1] == 'array srez':
  780.             if len(lastOperators[-1]) == 0:
  781.               value[-1][-1] = findVariableValue(name[-1])
  782.             else:
  783.               value[-1][-1] = math(value[-1][-1], findVariableValue(name[-1]), lastOperators[-1][-1])
  784.           if dataType(value[-1][-1]) == 'str':
  785.             debug += [conv(value[-1][-1])]
  786.           else:
  787.             debug += [value[-1][-1]]
  788.           print(*debug)
  789.           return
  790.  
  791.         if e == ',' and lastElement[-1] == 'math oper' and globalElement[-1] == 'print':
  792.           if lastElement[-1] == 'array srez':
  793.             if len(lastOperators[-1]) == 0:
  794.               value[-1][-1] = findVariableValue(name[-1])
  795.             else:
  796.               value[-1][-1] = math(value[-1][-1], findVariableValue(name[-1]), lastOperators[-1][-1])
  797.           if dataType(value[-1][-1]) == 'str':
  798.             debug += [conv(value[-1][-1])]
  799.           else:
  800.             debug += [value[-1][-1]]
  801.           lastElement = ['math first']
  802.  
  803.           continue
  804.         if lastElement[-1] == 'math oper' and operators.count(e) > 0:
  805.             if operators.count(e) > 0:
  806.               lastOperators[-1] += [e]
  807.               lastElement[-1] = 'math'
  808.               continue
  809.             elif dataType(e) == 'array':
  810.               code = getArray(e, ':')
  811.               if main[main.index(e) - 1] == ")" or len(lastArrayName) == 0:
  812.                 if isIteble(value[-1][-1]):
  813.                   ex = value[-1][-1]
  814.                 else:
  815.                   continue
  816.               else:
  817.                 ex = findVariableValue(lastArrayName[-1])
  818.               ex.pop()
  819.               resultVar = None
  820.               #print(code)
  821.               if len(code) == 1:
  822.                   resultVar = ex[int(code[0])]
  823.               elif len(code) == 2:
  824.                   resultVar = ex[int(code[0]):int(code[1])]
  825.               elif len(code) == 3:
  826.                   resultVar = ex[int(code[0]):int(code[1]):int(code[2])]
  827.               if len(lastOperators[-1]) == 0:
  828.                   if lastElement[-1] == 'math first':
  829.                       value[-1][-1] = resultVar
  830.                         #print(value)
  831.                   else:
  832.                       value[-1] = [resultVar]
  833.               else:
  834.                   #print(resultVar, e, globalElement, lastElement, lastOperators, value)
  835.                   value[-1][-1] = math(value[-1][-1], resultVar, lastOperators[-1][-1])
  836.                   lastOperators[-1].pop()
  837.               lastElement[-1] = 'math oper'
  838.               continue
  839.  
  840.         #print(name, value, lastElement, globalElement)
  841.        
  842.         if e == '(':
  843.           error("Error: Invalid function '" + name[-1] + "'!")
  844.         else:
  845.           error("Error: Invalid function '" + e + "'!")
  846.         return 'error'
  847.  
  848.     if len(main) > 1 and len(globalElement) > 0:
  849.         funcId = globalElement[-1]
  850.         if funcId == 'variable':
  851.             if findVariableValue(name[-1]) == None:
  852.                 newVariable(name[-1], value[-1][-1], dataType(value[-1][-1]))
  853.             else:
  854.                 if arraySrez != [] and isIteble(findVariableValue(name[-1])):
  855.                   value = value[-1][-1]
  856.                   startValue = findVariableValue(name[-1])
  857.                   count = len(arraySrez)
  858.                   if count == 1:
  859.                     startValue[arraySrez[0]] = value[-1][-1]
  860.                   elif count == 2:
  861.                     startValue[arraySrez[0]:arraySrez[1]] = value[-1][-1]
  862.                   elif count == 3:
  863.                     startValue[arraySrez[0]:arraySrez[1]:arraySrez[2]] = value[-1][-1]
  864.                   reloadVariableValue(name[-1], startValue, 'list')
  865.                 else:
  866.                   reloadVariableValue(name[-1], value[-1][-1], dataType(value[-1][-1]))
  867.         if funcId == 'print':
  868.             error("Error: Expected ')'!")
  869.             return 'error'
  870.         if funcId == 'return':
  871.             return value[-1][-1]
  872.    
  873.  
  874.  
  875. lineEndWithout = ['\t', ' ', '\n']
  876. lineEndWith = ['(', ')', ',', ':']
  877.  
  878. def loadToLines(code):
  879.     result = []
  880.     text = ''
  881.     asc = 0
  882.     asc2 = 0
  883.     for e in code:
  884.         if e == '\n' and asc == 0 and asc2 == 0:
  885.             result += [text]
  886.             text = ''
  887.             continue
  888.         elif e == '{':
  889.           asc += 1
  890.         elif e == '}':
  891.           asc -= 1
  892.         elif e == '[':
  893.           asc2 += 1
  894.         elif e == ']':
  895.           asc2 -= 1
  896.         text += e
  897.     if text != '':
  898.         result += [text]
  899.     return result
  900.  
  901. def loadToLine(code):
  902.     result = []
  903.     text = ''
  904.     isString = False
  905.     asc = 0
  906.     asc2 = 0
  907.     for e in code:
  908.         if isString == False and asc == 0 and e != '{' and e != '}'and e != '[' and e != ']' and asc2 == 0:
  909.             if lineEndWithout.count(e) != 0:
  910.                 if text != '':
  911.                     result += [text]
  912.                     text = ''
  913.                 continue
  914.             if lineEndWith.count(e) != 0:
  915.                 if text != '':
  916.                     result += [text]
  917.                     text = ''
  918.                 result += [e]
  919.                 continue
  920.         elif e == '{' and isString == False and asc2 == 0:
  921.             if asc == 0:
  922.                 if text != '':
  923.                     result += [text]
  924.                     text = ''
  925.             asc += 1
  926.         elif e == '}' and isString == False and asc2 == 0:
  927.             if asc == 1:
  928.               result += [text + '}']
  929.               text = ''
  930.               asc -= 1
  931.               continue
  932.             asc -= 1
  933.         elif e == '[' and isString == False and asc == 0:
  934.             if asc2 == 0:
  935.                 if text != '':
  936.                     result += [text]
  937.                     text = ''
  938.             asc2 += 1
  939.         elif e == ']' and isString == False and asc == 0:
  940.             if asc2 == 1:
  941.               result += [text + ']']
  942.               text = ''
  943.               asc2 -= 1
  944.               continue
  945.             asc -= 1
  946.         if e == "\"" and asc == 0 and asc2 == 0:
  947.             if isString:
  948.                 result += ['\"' + text + '\"']
  949.                 text = ''
  950.             isString = not isString
  951.             continue
  952.         text += e
  953.  
  954.     if text != '':
  955.         result += [text]
  956.     return result
  957.  
  958. programm = open('programm.ncd', 'r').read()
  959.  
  960. def startCompile(programm, line):
  961.     codeRET = loadToLines(programm)
  962.     idx = 0
  963.     for e in codeRET:
  964.         pr = loadToLine(str(e))
  965.         output = compileNanoCode(pr, idx + line)
  966.         if output == 'error':
  967.             break;
  968.         if output != None:
  969.             return output
  970.         idx += e.count('\n') + 1
  971.  
  972. startCompile(programm, 0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement