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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
|
/*
* Copyright (C) 2004-2008, Parrot Foundation.
*/
/*
=head1 NAME
examples/compiler/japhc.c
=head1 DESCRIPTION
example compiler used by japh16.pasm
=head1 SYNOPSIS
$ make -C examples/compilers/
$ parrot examples/japh/japh16.pasm
=head2 Functions
=over 4
=cut
*/
#include "parrot/parrot.h"
#define CONST_STRING(i, s) Parrot_str_new_constant((i), (s))
#define CONST_STRING_GEN(i, s) Parrot_str_new_constant((i), (s))
#include "pmc/pmc_sub.h"
#define C_DEBUG 0
#if C_DEBUG
# include <stdio.h>
# define cdebug(x) fprintf (x)
#else
# define cdebug(x)
#endif
INTVAL dynpmc_class_JaPHC;
PMC* japh_compiler(PARROT_INTERP, const char *s);
/*
=item C<void Parrot_lib_japhc_init(PARROT_INTERP, PMC* lib)>
loadlib calls the load and init hooks
we use init to register the compiler
=cut
*/
void
Parrot_lib_japhc_init(PARROT_INTERP, PMC* lib)
{
STRING *whoami;
cdebug((stderr, "japhc_init\n"));
whoami = CONST_STRING_GEN(interp, "JaPHC");
dynpmc_class_JaPHC = Parrot_pmc_register_new_type(interp, whoami);
/* Parrot_JaPHC_class_init(interp, dynpmc_class_JaPHC, 0); */
/* Parrot_compreg(interp, whoami, japh_compiler); */
}
/*
=item C<static int unescape(char *string)>
Unescape a string.
=cut
*/
static int
unescape(char *string)
{
char *start, *p;
for (start = p = string ; *string; string++) {
if (*string == '\\' && string[1]) {
switch (*++string) {
case 'n':
*p++ = '\n';
break;
default:
*p++ = *string;
break;
}
}
else
*p++ = *string;
}
*p = 0;
return p - start;
}
/*
=item C<static int add_const_str(PARROT_INTERP, PackFile_ConstTable *consts,
char *str)>
add constant string to constant_table
=cut
*/
static int
add_const_str(PARROT_INTERP, PackFile_ConstTable *consts, char *str)
{
int k, l;
char *o;
char *buf = o = strdup(str);
/*
* TODO strip delimiters in lexer, this needs adjustment in printint strings
*/
if (*buf == '"') {
buf++;
l = unescape(buf);
if (l)
buf[--l] = '\0';
}
else if (*buf == '\'') {
buf++;
l = strlen(buf);
if (l)
buf[--l] = '\0';
}
else {
l = unescape(buf);
}
/* Update the constant count and reallocate */
k = ++consts->const_count;
if (consts->constants == NULL)
consts->constants = mem_sys_allocate(
k * sizeof (PackFile_Constant *));
else
consts->constants = mem_sys_realloc(consts->constants,
k * sizeof (PackFile_Constant *));
/* Allocate a new constant */
consts->constants[--k] = PackFile_Constant_new(interp);
consts->constants[k]->type = PFC_STRING;
consts->constants[k]->u.string = Parrot_str_new_init(interp, buf,
(UINTVAL) l, Parrot_latin1_encoding_ptr, 0);
free(o);
return k;
}
/*
=item C<PMC* japh_compiler(PARROT_INTERP, const char *program)>
simple compiler - no error checking
=cut
*/
PMC*
japh_compiler(PARROT_INTERP, const char *program)
{
PackFile_ByteCode *cur_cs, *old_cs;
PackFile_ConstTable *consts;
opcode_t* pc;
const char *p;
PMC *sub;
Parrot_sub *sub_data;
#define CODE_SIZE 128
cdebug((stderr, "japh_compiler '%s'\n", program));
/*
* need some packfile segments
*/
cur_cs = PF_create_default_segs(interp, "JAPHc", 1);
old_cs = Parrot_switch_to_cs(interp, cur_cs, 0);
/*
* alloc byte code mem
*/
cur_cs->base.data = mem_sys_allocate(CODE_SIZE * sizeof (opcode_t));
cur_cs->base.size = CODE_SIZE;
consts = cur_cs->const_table;
/*
* now start compiling
*/
pc = cur_cs->base.data;
for (p = program; *p; ++p) {
switch (*p) {
case 'p': /* print_sc */
*pc++ = interp->op_lib->op_code("print_sc", 1);
/* const follows */
++p;
switch (*p) {
case 'J':
*pc++ = add_const_str(interp, consts, "Just ");
break;
case 'a':
*pc++ = add_const_str(interp, consts, "another ");
break;
case 'P':
*pc++ = add_const_str(interp, consts, "Parrot ");
break;
case 'H':
*pc++ = add_const_str(interp, consts, "Hacker");
break;
case 'n':
*pc++ = add_const_str(interp, consts, "\n");
break;
}
break;
case 'e': /* end */
*pc++ = interp->op_lib->op_code("invoke_p", 1);
*pc++ = 1;
break;
}
}
if (old_cs) {
/* restore old byte_code, */
(void)Parrot_switch_to_cs(interp, old_cs, 0);
}
/*
* create sub PMC
*/
sub = pmc_new(interp, enum_class_Eval);
PMC_get_sub(interp, sub, sub_data);
sub_data->seg = cur_cs;
sub_data->address = cur_cs->base.data;
sub_data->end = cur_cs->base.data + cur_cs->base.size;
sub_data->name = string_from_literal(interp, "JaPHC");
return sub;
}
/*
=back
=cut
*/
/*
* Local variables:
* c-file-style: "parrot"
* End:
* vim: expandtab shiftwidth=4 cinoptions='\:2=2' :
*/
|