|
15 | 15 |
|
16 | 16 | (defvar *current-id*)
|
17 | 17 |
|
18 |
| -;;; ast = abstract syntax tree |
19 |
| -(defun abstract-sem-tree-gen (ast &key (curr-file 1)) |
20 |
| - (let ((*current-file-ver* curr-file) |
21 |
| - (*current-id* 0)) |
22 |
| - (top-level-rule ast))) |
| 18 | +(defparameter *semanitc-errors-list* nil) |
| 19 | + |
| 20 | +(defclass semantic-error-info () |
| 21 | + ((error-text :accessor error-text |
| 22 | + :initarg :error-text) |
| 23 | + (error-node :accessor error-node |
| 24 | + :initarg :error-node))) |
| 25 | + |
| 26 | +(defun abstract-sem-tree-gen (syn-tree &key (curr-file 1)) |
| 27 | + (let* ((*current-file-ver* curr-file) |
| 28 | + (*current-id* 0) |
| 29 | + (*semanitc-errors-list*) |
| 30 | + (res-ast (match-top syn-tree))) |
| 31 | + (values res-ast |
| 32 | + *semanitc-errors-list*))) |
23 | 33 |
|
24 | 34 | (defun get-id ()
|
25 | 35 | (prog1 *current-id*
|
26 | 36 | (incf *current-id*)))
|
27 | 37 |
|
28 |
| -(defun top-level-rule (ast) |
| 38 | +(defun match-top (root-of-syn-tree) |
29 | 39 | (destructuring-bind (term-type annotations &rest elements)
|
30 |
| - ast |
| 40 | + root-of-syn-tree |
31 | 41 | (declare (ignore term-type annotations))
|
32 |
| - (mapcar (lambda (el) |
33 |
| - (match-s-expr el)) |
| 42 | + (mapcar (lambda (term-el) |
| 43 | + (match-s-expr term-el)) |
34 | 44 | elements)))
|
35 | 45 |
|
36 |
| -(defun match-s-expr (el) |
37 |
| - (ecase (first el) |
38 |
| - ((:atom) (make-lexem-wrapper (third el))) |
39 |
| - ((:list) |
40 |
| - (or (match-defun el) |
41 |
| - (match-function-call el))))) |
| 46 | +(defun match-s-expr (term-el) |
| 47 | + (destructuring-bind (term-type annotations first-s-expr &rest others) |
| 48 | + term-el |
| 49 | + (ecase term-type |
| 50 | + ((:atom) (make-atom-node term-el)) |
| 51 | + ((:list) |
| 52 | + (unless (eq (get-term-type first-s-expr) :atom) |
| 53 | + (return-from match-s-expr |
| 54 | + (make-ill-node-and-push-to-errors-list |
| 55 | + "Illegal function call" |
| 56 | + term-el))) |
| 57 | + (unless (eq (get-atom-term-type first-s-expr) :symbol) |
| 58 | + (return-from match-s-expr |
| 59 | + (make-ill-node-and-push-to-errors-list |
| 60 | + "Illegal function call" |
| 61 | + term-el))) |
| 62 | + (let ((normal-symbol-string |
| 63 | + (get-symbol-atom-normal-string-form first-s-expr))) |
| 64 | + (cond |
| 65 | + ((string= normal-symbol-string "DEFUN") |
| 66 | + (match-defun first-s-expr annotations others)) |
| 67 | + (t (match-function-call first-s-expr annotations others)))))))) |
| 68 | + |
| 69 | +(defun make-ill-node-and-push-to-errors-list (error-text ill-term) |
| 70 | + (let ((ill-node (make-illegal-node ill-term :is-top? t)) |
| 71 | + (coord (ecase (get-term-type ill-term) |
| 72 | + ((:atom) (get-atom-term-position ill-term)) |
| 73 | + ((:list) (rest (assoc :lparen-coord (second ill-term))))))) |
| 74 | + (push (make-instance 'semantic-error-info |
| 75 | + :error-text |
| 76 | + (format nil "At (~S:~S) ~A" |
| 77 | + (first coord) |
| 78 | + (second coord) |
| 79 | + error-text) |
| 80 | + :error-node (id ill-node)) |
| 81 | + *semanitc-errors-list*) |
| 82 | + ill-node)) |
42 | 83 |
|
43 | 84 | (defun is-atom-s-expr? (s-expr)
|
44 | 85 | (eq (first s-expr) :atom))
|
45 |
| - |
46 | 86 | (defun get-form*-vector (form*-list)
|
47 | 87 | (when form*-list
|
48 | 88 | (map 'vector #'match-s-expr form*-list)))
|
49 | 89 |
|
50 |
| -(defun make-lexem-wrapper (lexem) |
51 |
| - (make-instance 'lexem-wrapper-node |
52 |
| - :lexem-info lexem |
| 90 | +(defun get-term-type (term) |
| 91 | + (first term)) |
| 92 | + |
| 93 | +(defun get-atom-term-type (atom-term) |
| 94 | + (lexer:lexem-type (third atom-term))) |
| 95 | + |
| 96 | +(defun get-atom-term-position (atom-term) |
| 97 | + (let ((lex-info (third atom-term))) |
| 98 | + (list (lexer:lexem-line lex-info) |
| 99 | + (lexer:lexem-column lex-info)))) |
| 100 | + |
| 101 | +(defun get-symbol-atom-normal-string-form (atom-term) |
| 102 | + (string-upcase (lexem-string (third atom-term)))) |
| 103 | + |
| 104 | +(defun make-atom-node (atom-term &key (atom-type 'simple-atom-node)) |
| 105 | + (make-instance atom-type |
| 106 | + :lexem-info (third atom-term) |
53 | 107 | :id (get-id)))
|
54 | 108 |
|
55 |
| -(defun match-defun (list-element) |
56 |
| - (let ((thrd (third list-element))) |
57 |
| - (when (and (is-atom-s-expr? thrd) |
58 |
| - (is-lexem-symbol?= (third thrd) "defun") |
59 |
| - (>= (length list-element) 6)) |
60 |
| - (destructuring-bind (type par-info keyword name parms &rest forms) |
61 |
| - list-element |
62 |
| - (declare (ignore type)) |
| 109 | +(defun make-illegal-node (term &key is-top?) |
| 110 | + (let ((node-id (get-id))) |
| 111 | + (destructuring-bind (term-type annotations &rest others) |
| 112 | + term |
| 113 | + (ecase term-type |
| 114 | + ((:atom) (make-atom-node term)) |
| 115 | + ((:list) (make-instance 'illegal-node |
| 116 | + :id node-id |
| 117 | + :parenthesis-info annotations |
| 118 | + :elements (map 'vector |
| 119 | + #'make-illegal-node |
| 120 | + others) |
| 121 | + :is-top? is-top?)))))) |
| 122 | + |
| 123 | +(defun match-defun (keyword-term par-info elements) |
| 124 | + (handler-case |
| 125 | + (destructuring-bind (func-name parms &rest forms) |
| 126 | + elements |
63 | 127 | (let* ((node-id (get-id))
|
64 | 128 | (res-obj
|
65 |
| - (make-instance |
66 |
| - 'defun-node |
67 |
| - :keyword-lexem (make-lexem-wrapper (third keyword)) |
68 |
| - :func-name (make-lexem-wrapper (third name)) |
69 |
| - :parenthesis-info par-info |
70 |
| - :parameters-list (gen-list-node parms) |
71 |
| - :body-forms (get-form*-vector forms) |
72 |
| - :id node-id))) |
73 |
| - (add-to-stats (lexem-string (third name)) |
| 129 | + (make-instance |
| 130 | + 'defun-node |
| 131 | + :keyword-lexem (make-atom-node keyword-term :atom-type 'keyword-atom) |
| 132 | + :func-name (make-atom-node func-name :atom-type 'func-name-atom) |
| 133 | + :parenthesis-info par-info |
| 134 | + :parameters-list (make-list-node parms) |
| 135 | + :body-forms (get-form*-vector forms) |
| 136 | + :id node-id))) |
| 137 | + (add-to-stats (get-symbol-atom-normal-string-form func-name) |
74 | 138 | res-obj
|
75 | 139 | :stat-name :defuns
|
76 | 140 | :file-ver *current-file-ver*)
|
77 |
| - res-obj))))) |
| 141 | + res-obj)) |
| 142 | + (error (c) |
| 143 | + (declare (ignore c)) |
| 144 | + (make-ill-node-and-push-to-errors-list |
| 145 | + "incorrect defun" |
| 146 | + `(:list ,par-info ,keyword-term ,@elements))))) |
78 | 147 |
|
79 |
| -(defun gen-list-node (list-element) |
| 148 | +(defun make-list-node (list-element) |
80 | 149 | (when (eq (first list-element) :list)
|
81 | 150 | (let ((node-id (get-id)))
|
82 | 151 | (make-instance 'list-node
|
83 | 152 | :parenthesis-info (second list-element)
|
84 | 153 | :elements (get-form*-vector (rest (rest list-element)))
|
85 | 154 | :id node-id))))
|
86 | 155 |
|
87 |
| -(defun match-function-call (list-element) |
88 |
| - (destructuring-bind (type par-info func-form &rest args) |
89 |
| - list-element |
90 |
| - (declare (ignore type)) |
91 |
| - (let ((node-id (get-id))) |
92 |
| - (make-instance 'function-call-node |
93 |
| - :func-lexem (match-s-expr func-form) |
94 |
| - :parenthesis-info par-info |
95 |
| - :func-arg-forms (get-form*-vector args) |
96 |
| - :id node-id)))) |
| 156 | +(defun match-function-call (func-name-term par-info func-args) |
| 157 | + (let ((node-id (get-id))) |
| 158 | + (make-instance 'function-call-node |
| 159 | + :func-lexem (make-atom-node func-name-term :atom-type 'func-name-atom) |
| 160 | + :parenthesis-info par-info |
| 161 | + :func-arg-forms (get-form*-vector func-args) |
| 162 | + :id node-id))) |
0 commit comments