4 * Copyright (C) 2004-2008, Parrot Foundation.
11 examples/compiler/japhc.c
15 example compiler used by japh16.pasm
20 $ make -C examples/compilers/
21 $ parrot examples/japh/japh16.pasm
31 #include "parrot/parrot.h"
32 #include "parrot/embed.h"
33 #include "../../src/pmc/pmc_sub.h"
39 # define cdebug(x) fprintf (x)
44 PMC
* japh_compiler(PARROT_INTERP
, const char *s
);
48 =item C<void Parrot_lib_japhc_init(PARROT_INTERP, PMC* lib)>
50 loadlib calls the load and init hooks
51 we use init to register the compiler
58 Parrot_lib_japhc_init(PARROT_INTERP
, PMC
* lib
)
62 cdebug((stderr
, "japhc_init\n"));
63 cmp
= Parrot_str_new_constant(interp
, "JaPH_Compiler");
64 Parrot_compreg(interp
, cmp
, japh_compiler
);
70 =item C<static int unescape(char *string)>
77 unescape(char *string
)
81 for (start
= p
= string
; *string
; string
++) {
82 if (*string
== '\\' && string
[1]) {
101 =item C<static int add_const_str(PARROT_INTERP, PackFile_ConstTable *consts,
104 add constant string to constant_table
111 add_const_str(PARROT_INTERP
, PackFile_ConstTable
*consts
, char *str
)
115 char *buf
= o
= strdup(str
);
118 * TODO strip delimiters in lexer, this needs adjustment in printint strings
126 else if (*buf
== '\'') {
136 /* Update the constant count and reallocate */
137 k
= ++consts
->const_count
;
138 if (consts
->constants
== NULL
)
139 consts
->constants
= mem_sys_allocate(
140 k
* sizeof (PackFile_Constant
*));
142 consts
->constants
= mem_sys_realloc(consts
->constants
,
143 k
* sizeof (PackFile_Constant
*));
145 /* Allocate a new constant */
146 consts
->constants
[--k
] = PackFile_Constant_new(interp
);
147 consts
->constants
[k
]->type
= PFC_STRING
;
148 consts
->constants
[k
]->u
.string
=
149 string_make(interp
, buf
, (UINTVAL
) l
, "iso-8859-1", 0);
156 =item C<PMC* japh_compiler(PARROT_INTERP, const char *program)>
158 simple compiler - no error checking
165 japh_compiler(PARROT_INTERP
, const char *program
)
167 PackFile_ByteCode
*cur_cs
, *old_cs
;
168 PackFile_ConstTable
*consts
;
172 Parrot_sub
*sub_data
;
174 #define CODE_SIZE 128
175 cdebug((stderr
, "japh_compiler '%s'\n", program
));
178 * need some packfile segments
180 cur_cs
= PF_create_default_segs(interp
, "JAPHc", 1);
181 old_cs
= Parrot_switch_to_cs(interp
, cur_cs
, 0);
183 * alloc byte code mem
185 cur_cs
->base
.data
= mem_sys_allocate(CODE_SIZE
* sizeof (opcode_t
));
186 cur_cs
->base
.size
= CODE_SIZE
;
187 consts
= cur_cs
->const_table
;
189 * now start compiling
191 pc
= cur_cs
->base
.data
;
192 for (p
= program
; *p
; ++p
) {
194 case 'p': /* print_sc */
195 *pc
++ = interp
->op_lib
->op_code("print_sc", 1);
200 *pc
++ = add_const_str(interp
, consts
, "Just ");
203 *pc
++ = add_const_str(interp
, consts
, "another ");
206 *pc
++ = add_const_str(interp
, consts
, "Parrot ");
209 *pc
++ = add_const_str(interp
, consts
, "Hacker");
212 *pc
++ = add_const_str(interp
, consts
, "\n");
217 *pc
++ = interp
->op_lib
->op_code("invoke_p", 1);
223 /* restore old byte_code, */
224 (void)Parrot_switch_to_cs(interp
, old_cs
, 0);
229 sub
= pmc_new(interp
, enum_class_Eval
);
230 PMC_get_sub(interp
, sub
, sub_data
);
231 sub_data
->seg
= cur_cs
;
232 sub_data
->address
= cur_cs
->base
.data
;
233 sub_data
->end
= cur_cs
->base
.data
+ cur_cs
->base
.size
;
234 sub_data
->name
= string_from_literal(interp
, "JaPHC");
248 * c-file-style: "parrot"
250 * vim: expandtab shiftwidth=4: