def parse_arg(arg): if arg[0].startswith("~\\"): if len(arg) < 2: logger.log( "error", "after the type judgement of an argument to a function, you must provide a name for the argument." ) return "error" type = FunctionReference(arg[0][2:]) name = arg[1] argument = Argument(name, type) return argument else: name = arg[0] argument = Argument(name, None) return argument
def parse_invocation(invo): # let a Constant if builtins["let"] == invo.name: if len(invo.args) % 2 != 0: logger.log( "error", "for each name provided to `let`, there must be an immediately following value to bind it to." ) return "error" name = parse_result(invo.args[0]) type = parse_result(invo.args[1]) let = Let(name, type) context.add(let) return let # set a Constant elif builtins["set"] == invo.name: if len(invo.args) % 2 != 0: logger.log( "error", "for each name provided to `set`, there must be an immediately following value to bind it to." ) return "error" name = parse_result(invo.args[0]) value = parse_helper(invo.args[1]) cons = Constant(name, value) context.add(cons) return cons # define a Function # opens a new context elif builtins["function"] == invo.name: if len(invo.args) < 2: logger.log( "error", "to define a function you must at least provide a name and a result." ) return "error" name = parse_result(invo.args[0]) args = [parse_arg(x) for x in invo.args[1:-1]] result = parse_result(invo.args[-1]) func = Function(name, args, result) context.add(func) return func # invoke a normal function else: name = invo.name args = [parse_result(x) for x in invo.args] invocation = Invocation(name, args) context.add(invocation) return invocation
def main(): # argument parser argparser = argparse.ArgumentParser() # arguments: positional argparser.add_argument("mode", help="[ html | pdf | update | info ]") argparser.add_argument("input", nargs="?", help="input file") # argument: optional argparser.add_argument("-o","--output", help="name for the output file") argparser.add_argument("-d","--debug", help="print debug logs", action="store_true") # parse arguments args = argparser.parse_args() # logger.log( "log" , args ) def check_input(): if not args.input: logger.log( "error" , "please provide an input .nty file" ) quit() # # compile # if "html" == args.mode or "pdf" == args.mode: check_input() with open(args.input, "r+") as file: string = "".join([ l for l in file ]) lexed = lexer.lex(string) parsed = parser.parse(lexed) # logger.log("log", parsed) # output html if "html" == args.mode: pass # output pdf elif "pdf" == args.mode: pass # # update from github # elif "update" == args.mode: # TODO logger.log( "msg" , "to update Notery, please run `sh .../Notery/update.sh`" ) # subprocess.call(["sh","~/git/Notery/install.sh"]) # # get info # elif "info" == args.mode: titler.print_title() # # invalid mode # else: logger.log( "error" , "'"+args.mode+"' is not a valid mode" )
def check_input(): if not args.input: logger.log( "error" , "please provide an input .nty file" ) quit()
def parse(lexed): context = Context() # args can have specified types def parse_arg(arg): if arg[0].startswith("~\\"): if len(arg) < 2: logger.log( "error", "after the type judgement of an argument to a function, you must provide a name for the argument." ) return "error" type = FunctionReference(arg[0][2:]) name = arg[1] argument = Argument(name, type) return argument else: name = arg[0] argument = Argument(name, None) return argument # for phrases def parse_phrase(arr): phrase = [parse_helper(x) for x in arr] return Phrase(phrase) # for when you need a phrase to return a value def parse_result(arr): return parse_phrase(arr).get_result() def parse_invocation(invo): # let a Constant if builtins["let"] == invo.name: if len(invo.args) % 2 != 0: logger.log( "error", "for each name provided to `let`, there must be an immediately following value to bind it to." ) return "error" name = parse_result(invo.args[0]) type = parse_result(invo.args[1]) let = Let(name, type) context.add(let) return let # set a Constant elif builtins["set"] == invo.name: if len(invo.args) % 2 != 0: logger.log( "error", "for each name provided to `set`, there must be an immediately following value to bind it to." ) return "error" name = parse_result(invo.args[0]) value = parse_helper(invo.args[1]) cons = Constant(name, value) context.add(cons) return cons # define a Function # opens a new context elif builtins["function"] == invo.name: if len(invo.args) < 2: logger.log( "error", "to define a function you must at least provide a name and a result." ) return "error" name = parse_result(invo.args[0]) args = [parse_arg(x) for x in invo.args[1:-1]] result = parse_result(invo.args[-1]) func = Function(name, args, result) context.add(func) return func # invoke a normal function else: name = invo.name args = [parse_result(x) for x in invo.args] invocation = Invocation(name, args) context.add(invocation) return invocation def parse_constantreference(cref): # have name and thats all name = cref.name consref = ConstantReference(name) context.add(consref) return consref def parse_functionreference(fref): # have a name and thats all name = fref.name funcref = FunctionReference(name) context.add(funcref) return funcref def parse_helper(x): if isinstance(x, lexer.FunctionInvocation): return parse_invocation(x) elif isinstance(x, lexer.ConstantReference): return parse_constantreference(x) elif isinstance(x, lexer.FunctionReference): return parse_functionreference(x) elif isinstance(x, list): return parse_phrase(x) elif isinstance(x, str): return Value(x, "String") else: return "error: " + str(x) for x in lexed.args[0]: logger.log("log", x) parse_helper(x)