;;; Hey, emacs(1), this is -*- Mode: Common-Lisp; -*-, got it? ;;; ;;; PAL --- PCFG Approximation and Parsing Library for DELPH-IN ;;; ;;; Copyright (c) 2009 -- 2012 Johan Benum Evensberget (johan.benum@gmail.com) ;;; ;;; This program is free software; you can redistribute it and/or modify it ;;; under the terms of the GNU Lesser General Public License as published by ;;; the Free Software Foundation; either version 2.1 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 Lesser General Public ;;; License for more details. ;;; (in-package :pcfg) (defstruct (symbol-table (:constructor make-symbol-table (&key (test #'equal) (forward (make-hash-table :test test)) (backward (make-array 512)) (size 512) (count 0)))) forward backward size count) (defmethod print-object ((object symbol-table) stream) (let ((n (hash-table-count (symbol-table-forward object)))) (format stream "#[Symbol Table (~d forward~p, ~d backward~p of ~s)]" n n (symbol-table-count object) (symbol-table-count object) (symbol-table-size object)))) (defun symbol-to-code (symbol &optional (table *pcfg-symbol-table*) &key rop) (or (gethash symbol (symbol-table-forward table)) (unless rop (let* ((i (symbol-table-count table))) (setf (gethash symbol (symbol-table-forward table)) i) (when (>= i (symbol-table-size table)) (setf (symbol-table-size table) (* 2 (symbol-table-size table))) (setf (symbol-table-backward table) (adjust-array (symbol-table-backward table) (symbol-table-size table)))) (setf (aref (symbol-table-backward table) i) symbol) (incf (symbol-table-count table)) i)))) (defun code-to-symbol (code &optional (table *pcfg-symbol-table*)) (when (< code (symbol-table-count table)) (aref (symbol-table-backward table) code))) (defun set-symbol-and-code (symbol code &optional (table *pcfg-symbol-table*)) (setf (gethash symbol (symbol-table-forward table)) code) (when (>= code (symbol-table-size table)) (setf (symbol-table-size table) (* 2 (symbol-table-size table))) (setf (symbol-table-backward table) (adjust-array (symbol-table-backward table) (symbol-table-size table)))) (setf (aref (symbol-table-backward table) code) symbol) (incf (symbol-table-count table)))