/* tripl/arch/arch_templ.c * * This file is part of tripl. * * tripl is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * tripl is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with tripl. If not, see */ #include #include #include #include #include #include #include #include #include char *psave; __arch_templ static int new_var(char *p, struct arch *); __arch_templ static int set_name(char *p, struct arch *); __arch_templ static int math_set_name(char *p, struct arch *); __arch_templ static int check_condition(char *p, struct arch *); __arch_templ int arch_do_keyword(char *p, struct arch *arch_fn) { psave = p; switch (*p) { case '$': /* '$ asdf = 0;' --- asdf = 0 * '$ asdf 0;' --- asdf = 0 * '$ asdf;' --- asdf = 0 * */ p++; _skip_spaces(p); new_var(p, arch_fn); break; case '!': if (!arch_fn->arch_stack_dump) exit(generic_error("unsupported 'stack-dump' in current mode", psave, lno)); arch_fn->arch_stack_dump(); break; default: if (set_name(p, arch_fn) + math_set_name(p, arch_fn) + check_condition(p, arch_fn) == 0) exit(generic_error("unknow keyword", psave, lno)); break; } return 0; } __arch_templ static int new_var(char *p, struct arch *arch_fn) { char *name; int type, out, attr; getword(p, &type, &out); if (type != WORDTYPE_NAME) exit(generic_error("a name for variable required", psave, lno)); name = strndup(p, out); p += out; _skip_spaces(p); getword(p, &type, &out); switch (type) { case WORDTYPE_KWORD: if (out != '=') exit(generic_error("keyword but it's not a '='", psave, lno)); ++p; _skip_spaces(p); getword(p, &type, &out); if (type != WORDTYPE_NR) exit(generic_error("a number required", psave, lno)); _skip_word(p); break; case WORDTYPE_NR: /* to attribute (if have) */ _skip_word(p); break; case 0: out = 0; break; default: exit(generic_error("bad word after variable name", psave, lno)); } attr = getattrmask(p, vardef_attrtab); arch_fn->arch_new_var(name, out, attr); /* the name can't be free */ return 0; } static int set_name(char *p, struct arch *arch_fn) { char *name; int type, out; getword(p, &type, &out); if (type != WORDTYPE_NAME) return 0; name = strndup(p, out); p += out; _skip_spaces(p); getword(p, &type, &out); if (type != WORDTYPE_KWORD || out != '=') return 0; p++; _skip_spaces(p); getword(p, &type, &out); if (type != WORDTYPE_NR) exit(generic_error("a number required", psave, lno)); arch_fn->arch_set_var(name, out, 0); free(name); return 1; } /* 9 -> a; */ static int math_set_name(char *p, struct arch *arch_fn) { char *name; int type, out, nr; getword(p, &type, &out); if (type != WORDTYPE_NR) return 0; nr = out; /* this requires a space */ _skip_word(p); getword(p, &type, &out); if (type != WORDTYPE_KWORD || out != '-') exit(generic_error("a '->' required", psave, lno)); p++; getword(p, &type, &out); if (type != WORDTYPE_KWORD || out != '>') exit(generic_error("a '->' required", psave, lno)); p++; _skip_spaces(p); getword(p, &type, &out); if (type != WORDTYPE_NAME) exit(generic_error("a name required", psave, lno)); name = strndup(p, out); arch_fn->arch_set_var(name, nr, 0); free(name); return 1; } /* # a - 10 * a : 10 { * # = 1 * 1: xxx; * # = 2 * 2: yyy; * # = 4 * 4: zzz; * } */ static int check_condition(char *p, struct arch *arch_fn) { struct condition cond; int type, out; getword(p, &type, &out); if (type == WORDTYPE_NR) { cond.nra = out; _skip_word(p); } else if (type == WORDTYPE_NAME) { cond.namea = strndup(p, out); p += out; } else return 0; _skip_spaces(p); getword(p, &type, &out); getword(p, &type, &out); if (type != WORDTYPE_KWORD || out != ':') return 0; p++; _skip_spaces(p); getword(p, &type, &out); if (type == WORDTYPE_NR) { cond.nrb = out; _skip_word(p); } else if (type == WORDTYPE_NAME) { cond.nameb = strndup(p, out); p += out; } else return 0; printf("condition: %d %d %s %s\n", cond.nra, cond.nrb, cond.namea, cond.nameb); return 1; }