📄 mix_parser.c
字号:
/* -*-c-*- -------------- mix_parser.c : * Implementation of the functions declared in mix_parser.h and * xmix_parser.h * ------------------------------------------------------------------ * Copyright (C) 2000, 2001 Free Software Foundation, Inc. * * This program 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 2 of the License, or * (at your option) any later version. * * This program 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 this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * */#include <string.h>#include "mix.h"#include "mix_code_file.h"#include "xmix_parser.h"/* The flex-generated scanner, according to file mix_scanner.l */extern mix_parser_err_tmix_flex_scan (mix_parser_t *parser);/*------------ mixparser.h functions -------------------------------------*//* error messages */static const gchar * const ERR_MESSAGE_[] = { N_("successful compilation"), N_("file not yet compiled"), N_("internal error"), N_("unable to open MIX source file"), N_("unable to open MIX output file"), N_("unexpected end of file"), N_("invalid location field"), N_("duplicated symbol"), N_("symbol too long"), N_("missing operator field"), N_("unexpected location symbol"), N_("invalid address field"), N_("invalid index field"), N_("invalid f-specification"), N_("invalid operation field"), N_("invalid expression"), N_("undefined symbol"), N_("mismatched parenthesis"), N_("unexpected f-specfication"), N_("missing symbol name"), N_("symbol is an instruction name"), N_("failed write access to code file"), N_("operand of ALF pseudo instruction has less than 5 chars"), N_("operand of ALF pseudo instruction has more than 5 chars")};static const guint NO_OF_MESSAGES_ = sizeof(ERR_MESSAGE_)/sizeof (gchar*);const gchar *mix_parser_err_string (mix_parser_err_t error){ return (error < NO_OF_MESSAGES_) ? _(ERR_MESSAGE_[error]) : NULL;}guint mix_parser_err_count (const mix_parser_t *parser){ return (parser) ? parser->err_count : 0;}guint mix_parser_warning_count (const mix_parser_t *parser){ return (parser) ? parser->warn_count : 0;}const gchar *mix_parser_src_file_base_name (const mix_parser_t *parser){ return (parser) ? mix_file_base_name (parser->in_file) : NULL;}const gchar *mix_parser_src_file_extension (const mix_parser_t *parser){ return (parser) ? mix_file_extension (parser->in_file) : NULL;}/* Create/destroy a mix_parser *//* compare function for the table of ins */static gintcompare_shorts_ (gconstpointer s1, gconstpointer s2){ mix_short_t a = (mix_short_t)GPOINTER_TO_UINT(s1); mix_short_t b = (mix_short_t)GPOINTER_TO_UINT (s2); if ( mix_short_sign (a) == mix_short_sign (b) ) return mix_short_magnitude (a) - mix_short_magnitude (b); else if ( mix_short_magnitude (a) == 0 && mix_short_magnitude (b) == 0 ) return 0; else if ( mix_short_is_positive (a) ) return 1; return -1;}mix_parser_t *mix_parser_new (const gchar *in_file){ mix_parser_t *result; mix_file_t *f = mix_file_new_with_def_ext (in_file, mix_io_READ, MIX_SRC_DEFEXT); if ( f == NULL ) return NULL; result = g_new (mix_parser_t, 1); result->symbol_table = mix_symbol_table_new (); result->ls_table = mix_symbol_table_new (); result->cur_ls = 0; result->future_refs = g_hash_table_new (g_str_hash, g_str_equal); result->ins_table = g_tree_new (compare_shorts_); if ( result->symbol_table == NULL || result->future_refs == NULL || result->ins_table == NULL || result->ls_table == NULL ) { mix_symbol_table_delete (result->symbol_table); mix_symbol_table_delete (result->ls_table); g_hash_table_destroy (result->future_refs); g_tree_destroy (result->ins_table); mix_file_delete (f); g_free (result); g_warning (_("No system resources")); return NULL; } result->in_file = f; result->loc_count = MIX_SHORT_ZERO; result->status = MIX_PERR_NOCOMP; result->err_line = 0; result->err_count = 0; result->warn_count = 0; return result;}static voiddelete_list_vals_ (gpointer key, gpointer value, gpointer data){ g_free (key); g_slist_free ((GSList*)value);}static intdelete_tree_vals_ (gpointer key, gpointer value, gpointer data){ g_free (value); return FALSE;}voidmix_parser_delete (mix_parser_t *parser){ g_return_if_fail (parser != NULL); /* clear the GSList values of future_refs and its keys */ g_hash_table_foreach (parser->future_refs, delete_list_vals_, NULL); /* clear the ins_node_'s of the ins tree */ g_tree_traverse (parser->ins_table, delete_tree_vals_, G_IN_ORDER, NULL); /* destroy the tree and hash tables */ g_tree_destroy (parser->ins_table); mix_symbol_table_delete (parser->symbol_table); mix_symbol_table_delete (parser->ls_table); g_hash_table_destroy (parser->future_refs); mix_file_delete (parser->in_file); g_free (parser);}/* Compile a mix source file */static gbooleanundef_warning_ (gpointer symbol, gpointer value, gpointer data){ mix_parser_log_error ((mix_parser_t *)data, MIX_PERR_UNDEF_SYM, 1, (const gchar *)symbol, TRUE); /* move the symbol to the symbol table */ mix_symbol_table_insert_static (((mix_parser_t *)data)->symbol_table, symbol, MIX_WORD_ZERO); return TRUE;}static voidupdate_future_refs_value_ (mix_parser_t *parser, const gchar *name, mix_short_t value){ GSList *list = NULL; gpointer key; g_assert (parser != NULL && name != NULL); if ( g_hash_table_lookup_extended (parser->future_refs, name, &key, (gpointer *)&list) ) { GSList *tmp = list; ins_node_ *node; while ( tmp != NULL ) { node = (ins_node_ *)g_tree_lookup (parser->ins_table,tmp->data); g_assert (node); if (mix_get_ins_address (node->ins) == 1) { value = mix_short_negative (value); node->ins = mix_word_set_field (node->ins, MIX_WORD_ZERO, mix_fspec_new (1,2)); } mix_word_add_address (node->ins, value); g_tree_insert (parser->ins_table, tmp->data, (gpointer)node); tmp = g_slist_next (tmp); } g_hash_table_remove (parser->future_refs, name); g_free (key); g_slist_free (list); }}#define update_future_refs_(parser,name) \ update_future_refs_value_(parser, name, (parser)->loc_count);static voidupdate_ls_ (gpointer symbol, gpointer value, gpointer parser){ /* add an instruction on current location and update refs to it */ mix_ins_t ins; mix_word_t w = (mix_word_t) GPOINTER_TO_UINT (value); mix_parser_t *par = (mix_parser_t *) parser; mix_word_to_ins_uncheck (w, ins); mix_parser_add_ins (par, &ins, 0); update_future_refs_ (par, (const gchar *)symbol); par->loc_count++;}mix_parser_err_tmix_parser_compile (mix_parser_t *parser){ g_return_val_if_fail (parser != NULL, MIX_PERR_INTERNAL); g_return_val_if_fail (parser->in_file != NULL, MIX_PERR_NOIN); g_return_val_if_fail (parser->symbol_table != NULL, MIX_PERR_INTERNAL); g_return_val_if_fail (parser->future_refs != NULL, MIX_PERR_INTERNAL); g_return_val_if_fail (parser->ins_table != NULL, MIX_PERR_INTERNAL); parser->status = mix_flex_scan (parser); if ( parser->status == MIX_PERR_OK ) { mix_symbol_table_foreach (parser->ls_table, update_ls_, (gpointer)parser); g_hash_table_foreach_remove (parser->future_refs, undef_warning_, (gpointer)parser); } return parser->status;}/* Write a compiled source to a code file */struct write_code_context_{ mix_code_file_t *file; mix_parser_t *parser;};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -