def match_quote(tokens): if len(tokens) < 2: return None if tokens[0].name != "QUOTE": return None match = match_expr(tokens[1:]) if match is not None: t = tokens[0] quote = syntax(sym("quote"), t.line_no, t.char_no) expr = linked_list([quote, match[0]]) return syntax(expr, t.line_no, t.char_no), match[1] + 1
def match_compound(tokens): if len(tokens) <= 1: return None if tokens[0].name != "LPAREN": return None output = [] index = 1 while index < len(tokens) - 1: match = match_expr(tokens[index:]) if match is None: break output.append(match[0]) index += match[1] if tokens[index].name != "RPAREN": return None output = linked_list(output) return syntax(output, tokens[0].line_no, tokens[0].char_no), index + 1
def syntax_to_list(ast): v = ast.value if isinstance(v, linked_list): return linked_list(list(map(syntax_to_list, v))) return v