@@ -4,28 +4,7 @@ import colors = require('colors/safe')
4
4
5
5
/** Debug Token print function */
6
6
export function logToken ( Token : Token ) : void {
7
- let nameColor : colors
8
-
9
- switch ( Token . type ) {
10
- case Type . CONSTANT :
11
- nameColor = colors . green
12
- break
13
- case Type . IDENTIFIER :
14
- nameColor = colors . cyan
15
- break
16
- case Type . PUNCTUATION :
17
- nameColor = colors . white
18
- break
19
- case Type . TYPE :
20
- case Type . RESERVED :
21
- nameColor = colors . blue
22
- break
23
- case Type . WHITESPACE :
24
-
25
- default :
26
- nameColor = colors . grey
27
- break
28
- }
7
+ let nameColor : colors = getColour ( Token )
29
8
30
9
console . log (
31
10
colors . blue ( ` >>` ) +
@@ -56,11 +35,19 @@ export function logTree(node: SyntaxTree, indent: number = 0, side: string = ">"
56
35
return
57
36
}
58
37
38
+ let nameColor : colors = getColour ( node . content )
39
+
40
+ let printName : boolean = ( Type [ node . content . type ] != node . content . name )
41
+ let printLex : boolean = ( node . content . lexeme != null )
42
+ && ( node . content . lexeme . toString ( ) . toLowerCase ( ) != node . content . name . toString ( ) . toLowerCase ( ) )
43
+
59
44
/* Infix traversal */
60
45
logTree ( node . argument1 , indent + 1 , '/' )
61
46
if ( node . content != null ) {
62
47
console . log (
63
- prefix + ( `[${ colors . magenta ( Type [ node . content . type ] ) } ] - ${ colors . cyan ( node . content . name ) } , ${ colors . grey ( node . content . lexeme ) } \t` )
48
+ prefix + ( `[${ nameColor ( Type [ node . content . type ] ) } ]` ) +
49
+ ( printName ? ( ` - ${ colors . cyan ( node . content . name ) } ` ) : "" ) +
50
+ ( printLex ? ( `, ${ colors . grey ( node . content . lexeme ) } \t` ) : "" )
64
51
)
65
52
} else {
66
53
console . log (
@@ -87,4 +74,34 @@ export function logError(message: string) {
87
74
export function logCode ( code : string ) {
88
75
console . log ( colors . yellow ( '[OUTPUT] ' ) + 'Compiled code:' )
89
76
console . log ( '\t' + code . split ( '\n' ) . join ( '\n\t' ) )
77
+ }
78
+
79
+ function getColour ( Token : Token ) : colors {
80
+ switch ( Token . type ) {
81
+ case Type . VARIABLE :
82
+ case Type . CONSTANT :
83
+ return colors . green
84
+
85
+ case Type . IDENTIFIER :
86
+ return colors . cyan
87
+
88
+ case Type . PUNCTUATION :
89
+ return colors . white
90
+
91
+ case Type . TYPE :
92
+ case Type . RESERVED :
93
+ return colors . blue
94
+
95
+ case Type . START :
96
+ case Type . END :
97
+ case Type . DECLARE :
98
+ case Type . DECLARE_ASSIGN :
99
+ case Type . ASSIGN :
100
+ return colors . magenta
101
+
102
+ case Type . WHITESPACE :
103
+
104
+ default :
105
+ return colors . grey
106
+ }
90
107
}
0 commit comments