OSDN Git Service

Working on README, put defs in joy.py
[joypy/Thun.git] / implementations / uvm-ncc / parser.py
1
2 u32 parse_head;
3
4 u32
5 make_non_list_node(u32 rest) {
6         // A this point text[parse_head:rest] is a term: symbol or int.
7         return push_symbol("foo", empty_list);
8 }
9
10
11 // Extract terms from the text until a closing bracket is found.
12 u32
13 parse_list(char *text)
14 {
15         // trim blanks
16         while (text[parse_head] && text[parse_head] == ' ') ++parse_head;
17
18         if (!text[parse_head]) {
19                 print_str("Missing ']' bracket. A");
20                 print_endl();
21                 error = MISSING_CLOSING_BRACKET;
22                 return 0;
23         };
24
25         // So now we want to collect all chars up until the
26         // next '[', ']', blank, or the end of the string.
27         u32 rest = parse_head;
28         while (text[rest]) {
29                 if (text[rest] == '[' || text[rest] == ']' || text[rest] == ' ')
30                         break;
31                 //print_str(text + rest);print_endl();
32                 ++rest;
33         }
34
35         if (!text[rest]) {
36                 print_str("Missing ']' bracket. B");
37                 print_endl();
38                 error = MISSING_CLOSING_BRACKET;
39                 return 0;
40         };
41
42         // A this point text[parse_head:rest] is a term: symbol or int.
43         // or it's empty
44         u32 diff = rest - parse_head;
45         u32 result = 0;
46         if (diff) {
47                 result = make_non_list_node(rest);
48                 parse_head = rest;
49         /*} else if ('[' == text[rest]) {*/
50         /*      parse_head = rest + 1;*/
51         /*      result = cons(parse_list(text), empty_list);*/
52         } else if (']' == text[rest]) {
53                 parse_head = rest + 1;
54                 result = empty_list;
55         }
56         tails[VALUE_OF(result)] = parse_list(text);
57         return result;
58 }
59
60 u32
61 parse(char *text)
62 {
63         parse_head = 0;
64
65         // trim blanks
66         while (text[parse_head] && text[parse_head] == ' ') ++parse_head;
67
68         if (!text[parse_head]) return empty_list;
69         
70         if ('[' == text[parse_head]) {
71                 ++parse_head;
72                 u32 list = parse_list(text);
73                 if (error != NO_ERROR)
74                         return 0;
75                 return list;
76                 /*foo = cons(list, foo);*/
77         }
78         /*if (']')*/
79         /*print_str(text + parse_head);*/
80         /*print_str(text + parse_head);*/
81 }