summaryrefslogtreecommitdiff
path: root/src/frontend/driver.c
blob: 52eb3f4187b89dd669c58c52f63ced3a18c4e752 (plain)
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
#include "lexer.h"
#include "parser.h"
#include "node.h"
#include <stdio.h>
#include <libguile.h>

/**
 * @param node a tree of node structs representing a C source file.
 * @return a scheme representation of node.
 */
SCM
node_to_scm(Node *node) {
  SCM ret = scm_list_1(scm_from_locale_symbol(node_types[node->type]));

  SCM field_value = SCM_BOOL_F;
  switch (node->type) {
  case FUNC:
    field_value = scm_from_locale_string(node->field.name);
    break;
  case EXPR:
    field_value = scm_from_locale_symbol(node_ops[node->field.op]);
    break;
  case CONST:
    field_value = scm_from_int32(node->field.val);
    break;
  default:
    ;
  }

  if (scm_is_true(field_value))
    ret = scm_append(scm_list_2(ret, scm_list_1(field_value)));

  SCM child;
  for (size_t i = 0; i < node->num_children; ++i) {
    child = node_to_scm(node->children[i]);
    ret = scm_append(scm_list_2(ret, scm_list_1(child)));
  }

  return ret;
}

/** \brief Parser driver for ull.
 *         Given F, returns an AST of the program represented as a guile s-exp, or #f on a parse error.
 *         if DO_PARSE is false, only performs lexing, returning #t on a success.
 * @param f a preprocessed C file to parse.
 * @param do_parse on false, do not perform the parsing stage.
 * @return a SCM list representing the parse tree, or #t if do_parse is #f and lexing is successful, or #f otherwise.
 */
SCM
file_to_ast_wrapper(SCM f, SCM do_parse)
{
  char *file = scm_to_locale_string(f);
  Node *root = NULL;
  SCM ret = SCM_BOOL_F;

  yyin = fopen(file, "r");
  if (yyin != NULL){
    if (scm_is_true(do_parse)) {
      if (yyparse(&root) == 0) {
	ret = node_to_scm(root);
	free_node(root);
      }
    } else {
      ret = SCM_BOOL_T;
      int token;
      while ((token = yylex()) != 0) {
	if (token == YYerror)
	  ret = SCM_BOOL_F;
      }
    }
  }

  fclose(yyin);
  free(file);
  return ret;
}

void
init_parser_driver()
{
  scm_c_define_gsubr("file->ast", 2, 0, 0, file_to_ast_wrapper);
  scm_c_export("file->ast", NULL);
}

void
scm_init_parser_driver_module() {
  scm_c_define_module("frontend driver", init_parser_driver, NULL);
}