New doc system done for core
[io.git] / libs / iovm / source / IoCompiler.c
blob74e40bd6c234eb6427db8e6a0c8b93a691512ce5
2 //metadoc Compiler copyright Steve Dekorte 2002
3 //metadoc Compiler license BSD revised
4 /*metadoc Compiler description
5 Contains methods related to the compiling code.
6 */
7 //metadoc Compiler category Core
9 #include "IoCompiler.h"
10 #include "IoNumber.h"
11 #include "IoMessage_parser.h"
12 #include "IoLexer.h"
13 #include "IoToken.h"
14 #include "IoList.h"
16 IoObject *IoCompiler_proto(void *state)
18 IoMethodTable methodTable[] = {
19 {"tokensForString", IoObject_tokensForString},
20 //{"messageForTokens", IoObject_messageForTokens},
21 {"messageForString", IoObject_messageForString},
22 {"messageForString2", IoObject_messageForString2},
23 {NULL, NULL},
26 IoObject *self = IoObject_new(state);
27 IoObject_setSlot_to_(self, IOSYMBOL("type"), IOSYMBOL("Compiler"));
28 IoObject_addMethodTable_(self, methodTable);
30 return self;
33 IoObject *IoObject_tokensForString(IoObject *self, IoObject *locals, IoMessage *m)
35 /*doc Compiler tokensForString(aString)
36 Returns a list of token objects lexed from the input string.
39 IoSymbol *text = IoMessage_locals_seqArgAt_(m, locals, 0);
40 IoList *tokensList = IoList_new(IOSTATE);
41 IoLexer *lexer = IoLexer_new();
42 IoSymbol *name = IOSYMBOL("name");
43 IoSymbol *line = IOSYMBOL("line");
44 IoSymbol *character = IOSYMBOL("character");
45 IoSymbol *type = IOSYMBOL("type");
47 IoLexer_string_(lexer, CSTRING(text));
48 IoLexer_lex(lexer);
50 if (IoLexer_errorToken(lexer))
52 IoSymbol *errorString = IOSYMBOL(IoLexer_errorDescription(lexer));
53 IoLexer_free(lexer);
54 IoState_error_(IOSTATE, NULL, "compile error: %s", CSTRING(errorString));
56 else
58 IoToken *t;
60 while ((t = IoLexer_pop(lexer)))
62 IoObject *tokenObject = IoObject_new(IOSTATE);
64 IoObject_setSlot_to_(tokenObject, name, IOSYMBOL(IoToken_name(t)));
65 IoObject_setSlot_to_(tokenObject, line, IONUMBER(IoToken_lineNumber(t)));
66 IoObject_setSlot_to_(tokenObject, character, IONUMBER(IoToken_charNumber(t)));
67 IoObject_setSlot_to_(tokenObject, type, IOSYMBOL(IoToken_typeName(t)));
69 IoList_rawAppend_(tokensList, tokenObject);
73 IoLexer_free(lexer);
75 return tokensList;
78 IoObject *IoObject_messageForTokens(IoObject *self, IoObject *locals, IoMessage *m)
80 /*doc Compiler messageForTokens(aList)
81 Returns the compiled message object for the given token list.
84 return m;
87 IoObject *IoObject_messageForString(IoObject *self, IoObject *locals, IoMessage *m)
89 /*doc Compiler messageForString(aString, optionalLabelString)
90 Returns the compiled message object for aString.
93 IoSymbol *string = IoMessage_locals_seqArgAt_(m, locals, 0);
94 IoSymbol *label = IoMessage_rawLabel(m);
95 IoObject *result = IONIL(self);
97 if (IoMessage_argCount(m) > 1)
99 label = IoMessage_locals_symbolArgAt_(m, locals, 1);
102 return IoMessage_newFromText_labelSymbol_(IOSTATE, CSTRING((IoSymbol *)string), (IoSymbol *)label);
106 IoObject *IoObject_messageForString2(IoObject *self, IoObject *locals, IoMessage *m)
108 IoLexer *lexer = IoLexer_new();
109 char *text = IoMessage_locals_cStringArgAt_(m, locals, 0);
110 IoMessage *msg;
112 IoLexer_string_(lexer, text);
113 IoLexer_lex(lexer);
115 msg = IoMessage_newParse(IOSTATE, lexer);
117 IoLexer_free(lexer);
118 return msg;