Browse Source

Better error system, Correct line and column, Simple AST and colors

tags/v0.7.2
champii 1 year ago
parent
commit
7fba53ca29
53 changed files with 1047 additions and 824 deletions
  1. 9
    4
      cli.go
  2. 4
    0
      docs/changelog.md
  3. 156
    135
      lib/ast/ast.go
  4. 133
    131
      lib/ast/ast.og
  5. 0
    38
      lib/ast/node.go
  6. 0
    27
      lib/ast/node.og
  7. 1
    1
      lib/ast/walker/desugar.go
  8. 1
    1
      lib/ast/walker/desugar.og
  9. 3
    3
      lib/ast/walker/gob_register.go
  10. 3
    3
      lib/ast/walker/gob_register.og
  11. 49
    7
      lib/ast/walker/print.go
  12. 42
    7
      lib/ast/walker/print.og
  13. 5
    4
      lib/ast/walker/returnable.go
  14. 5
    4
      lib/ast/walker/returnable.og
  15. 3
    3
      lib/ast/walker/template.go
  16. 3
    3
      lib/ast/walker/template.og
  17. 3
    2
      lib/ast/walker/template_apply.go
  18. 3
    2
      lib/ast/walker/template_apply.og
  19. 3
    2
      lib/ast/walker/template_generator.go
  20. 3
    2
      lib/ast/walker/template_generator.og
  21. 5
    4
      lib/ast/walker/template_parse.go
  22. 5
    4
      lib/ast/walker/template_parse.og
  23. 17
    7
      lib/ast/walker/template_usage.go
  24. 17
    7
      lib/ast/walker/template_usage.og
  25. 7
    7
      lib/ast/walker/type_checker.go
  26. 7
    7
      lib/ast/walker/type_checker.og
  27. 6
    6
      lib/ast/walker/walker.go
  28. 6
    6
      lib/ast/walker/walker.og
  29. 2
    1
      lib/common/config.go
  30. 2
    1
      lib/common/config.og
  31. 21
    0
      lib/common/error.go
  32. 18
    0
      lib/common/error.og
  33. 12
    7
      lib/common/file.go
  34. 6
    3
      lib/common/file.og
  35. 67
    0
      lib/common/node.go
  36. 48
    0
      lib/common/node.og
  37. 39
    1
      lib/common/printer.go
  38. 27
    1
      lib/common/printer.og
  39. 3
    5
      lib/og/compile.go
  40. 3
    5
      lib/og/compile.og
  41. 10
    11
      lib/og/og.go
  42. 10
    11
      lib/og/og.og
  43. 7
    39
      lib/og/parse.go
  44. 8
    45
      lib/og/parse.og
  45. 4
    6
      lib/og/pool.go
  46. 4
    25
      lib/og/pool.og
  47. 10
    7
      lib/og/preproc.go
  48. 12
    11
      lib/og/preproc.og
  49. 114
    102
      lib/translator/translator.go
  50. 112
    107
      lib/translator/translator.og
  51. 3
    2
      main.go
  52. 1
    4
      tests/exemples/test.og
  53. 5
    3
      tests/og_test.go

+ 9
- 4
cli.go View File

@@ -4,20 +4,21 @@ import (
4 4
 	"os"
5 5
 	"time"
6 6
 
7
-	"github.com/champii/og/lib/og"
7
+	"github.com/champii/og/lib/common"
8 8
 
9 9
 	"github.com/urfave/cli"
10 10
 )
11 11
 
12
-func parseArgs(done func(og.OgConfig)) {
12
+func parseArgs(done func(*common.OgConfig)) {
13 13
 	cli_ := setupCli()
14 14
 
15 15
 	cli_.Action = func(c *cli.Context) error {
16
-		options := og.OgConfig{
16
+		options := common.OgConfig{
17 17
 			Blocks:      c.Bool("b"),
18 18
 			Dirty:       c.Bool("d"),
19 19
 			Print:       c.Bool("p"),
20 20
 			Ast:         c.Bool("a"),
21
+			SimpleAst:   c.Bool("s"),
21 22
 			Quiet:       c.Bool("q"),
22 23
 			Workers:     c.Int("w"),
23 24
 			OutPath:     c.String("o"),
@@ -28,7 +29,7 @@ func parseArgs(done func(og.OgConfig)) {
28 29
 			Paths:       []string(c.Args()),
29 30
 		}
30 31
 
31
-		done(options)
32
+		done(&options)
32 33
 
33 34
 		return nil
34 35
 	}
@@ -117,6 +118,10 @@ COPYRIGHT:
117 118
 			Usage: "Print the generated AST",
118 119
 		},
119 120
 		cli.BoolFlag{
121
+			Name:  "s, simple-ast",
122
+			Usage: "Print a simple AST instead with only terminals",
123
+		},
124
+		cli.BoolFlag{
120 125
 			Name:  "i, interpreter",
121 126
 			Usage: "Run a small interpreter (ALPHA)",
122 127
 		},

+ 4
- 0
docs/changelog.md View File

@@ -1,6 +1,10 @@
1 1
 # Changelog
2 2
 
3 3
 ## DEV: Current version
4
+  - Add `-s` option to show a simplified AST with only terminal nodes
5
+  - Errors show now the good original lines and columns
6
+  - Show an error when a template in unknown
7
+  - Colorful ast and lines/columns are shown
4 8
 
5 9
 ## v0.7.1
6 10
   - Fix `-p` that showed the non-formated output 

+ 156
- 135
lib/ast/ast.go
File diff suppressed because it is too large
View File


+ 133
- 131
lib/ast/ast.og View File

@@ -2,9 +2,10 @@
2 2
 
3 3
 import
4 4
 	strings
5
+	"github.com/champii/og/lib/common"
5 6
 
6 7
 struct SourceFile
7
-	*Node
8
+	*common.Node
8 9
 	Package  *Package
9 10
 	Import   *Import
10 11
 	TopLevels []*TopLevel
@@ -17,12 +18,12 @@ struct SourceFile
17 18
 		res
18 19
 
19 20
 struct Package
20
-	*Node
21
+	*common.Node
21 22
 	Name string
22 23
 	Eval: string -> "package " + @Name
23 24
 
24 25
 struct Import
25
-	*Node
26
+	*common.Node
26 27
 	Items []*ImportSpec
27 28
 	Eval: string ->
28 29
 		res := "import (\n"
@@ -31,7 +32,7 @@ struct Import
31 32
 		res + "\n)\n"
32 33
 
33 34
 struct ImportSpec
34
-	*Node
35
+	*common.Node
35 36
 	Path  string
36 37
 	Alias string
37 38
 	Eval: string ->
@@ -39,7 +40,7 @@ struct ImportSpec
39 40
 		else               => @Path
40 41
 
41 42
 struct TopLevel
42
-	*Node
43
+	*common.Node
43 44
 	Declaration  *Declaration
44 45
 	FunctionDecl *FunctionDecl
45 46
 	MethodDecl   *MethodDecl
@@ -49,7 +50,7 @@ struct TopLevel
49 50
 		else                         => @MethodDecl.Eval()
50 51
 
51 52
 struct Declaration
52
-	*Node
53
+	*common.Node
53 54
 	ConstDecl *ConstDecl
54 55
 	TypeDecl  *TypeDecl
55 56
 	VarDecl   *VarDecl
@@ -59,7 +60,7 @@ struct Declaration
59 60
 		else                      => @VarDecl.Eval()
60 61
 
61 62
 struct ConstDecl
62
-	*Node
63
+	*common.Node
63 64
 	ConstSpecs []*ConstSpec
64 65
 	Eval: string ->
65 66
 		res := "const ("
@@ -68,18 +69,19 @@ struct ConstDecl
68 69
 		res + ")"
69 70
 
70 71
 struct ConstSpec
71
-	*Node
72
-	IdentifierList []string
72
+	*common.Node
73
+	IdentifierList *IdentifierList
73 74
 	Type           *Type
74 75
 	ExpressionList *ExpressionList
75 76
 	Eval: string ->
76
-		res := strings.Join(@IdentifierList, ",")
77
+		res := ""
78
+		if @IdentifierList != nil => res += @IdentifierList.Eval()
77 79
 		if @Type           != nil => res += " " + @Type.Eval()
78 80
 		if @ExpressionList != nil => res += " = " + @ExpressionList.Eval()
79 81
 		res
80 82
 
81 83
 struct ExpressionList
82
-	*Node
84
+	*common.Node
83 85
 	Expressions []*Expression
84 86
 	Eval: string ->
85 87
 		res := ""
@@ -90,7 +92,7 @@ struct ExpressionList
90 92
 		res
91 93
 
92 94
 struct Parameters
93
-	*Node
95
+	*common.Node
94 96
 	List []*Parameter
95 97
 	Eval: string ->
96 98
 		res := "("
@@ -101,7 +103,7 @@ struct Parameters
101 103
 		res + ")"
102 104
 
103 105
 struct TypeDecl
104
-	*Node
106
+	*common.Node
105 107
 	TypeSpecs     []*TypeSpec
106 108
 	StructType    *StructType
107 109
 	InterfaceType *InterfaceType
@@ -120,13 +122,13 @@ struct TypeDecl
120 122
 		res
121 123
 
122 124
 struct TypeSpec
123
-	*Node
125
+	*common.Node
124 126
 	Name string
125 127
 	Type *Type
126 128
 	Eval: string -> @Name + " " + @Type.Eval()
127 129
 
128 130
 struct FunctionDecl
129
-	*Node
131
+	*common.Node
130 132
 	Name      string
131 133
 	Function  *Function
132 134
 	Signature *Signature
@@ -137,13 +139,13 @@ struct FunctionDecl
137 139
 		res
138 140
 
139 141
 struct Function
140
-	*Node
142
+	*common.Node
141 143
 	Signature *Signature
142 144
 	Block     *Block
143 145
 	Eval: string ->  @Signature.Eval() + @Block.Eval()
144 146
 
145 147
 struct MethodDecl
146
-	*Node
148
+	*common.Node
147 149
 	Receiver  *Receiver
148 150
 	Function  *Function
149 151
 	Signature *Signature
@@ -154,7 +156,7 @@ struct MethodDecl
154 156
 		res
155 157
 
156 158
 struct Receiver
157
-	*Node
159
+	*common.Node
158 160
 	Package           string
159 161
 	IsPointerReceiver bool
160 162
 	Method            string
@@ -166,7 +168,7 @@ struct Receiver
166 168
 		res
167 169
 
168 170
 struct VarDecl
169
-	*Node
171
+	*common.Node
170 172
 	VarSpecs []*VarSpec
171 173
 	Eval: string ->
172 174
 		res := "var ("
@@ -175,20 +177,21 @@ struct VarDecl
175 177
 		res + ")"
176 178
 
177 179
 struct VarSpec
178
-	*Node
179
-	IdentifierList []string
180
+	*common.Node
181
+	IdentifierList *IdentifierList
180 182
 	Type           *Type
181 183
 	ExpressionList *ExpressionList
182 184
 	Statement      *Statement
183 185
 	Eval: string ->
184
-		res := strings.Join(@IdentifierList, ",")
186
+		res := ""
187
+		if @IdentifierList != nil => res += @IdentifierList.Eval()
185 188
 		if @Type           != nil => res += " " + @Type.Eval()
186 189
 		if @ExpressionList != nil => res += "=" + @ExpressionList.Eval()
187 190
 		if @Statement      != nil => res += "=" + @Statement.Eval()
188 191
 		res
189 192
 
190 193
 struct Block
191
-	*Node
194
+	*common.Node
192 195
 	Statements []*Statement
193 196
 	Eval: string ->
194 197
 		res := "{\n"
@@ -204,16 +207,16 @@ struct Block
204 207
 				last.IfStmt.AddReturn()
205 208
 			if last.SimpleStmt != nil
206 209
 				@Statements[len(@Statements)-1] = &Statement
207
-					Node: NewNodeNoCtx()
210
+					Node: common.NewNodeNoCtx(&Statement{})
208 211
 					ReturnStmt: &ReturnStmt
209
-						Node: NewNodeNoCtx()
212
+						Node: common.NewNodeNoCtx(&ReturnStmt{})
210 213
 						Expressions: &ExpressionList
211
-							Node: NewNodeNoCtx()
214
+							Node: common.NewNodeNoCtx(&ExpressionList{})
212 215
 							Expressions: []*Expression{last.SimpleStmt.Expression}
213 216
 
214 217
 
215 218
 struct Statement
216
-	*Node
219
+	*common.Node
217 220
 	SimpleStmt      *SimpleStmt
218 221
 	LabeledStmt     *LabeledStmt
219 222
 	GoStmt          *GoStmt
@@ -247,7 +250,7 @@ struct Statement
247 250
 		else                            => @Declaration.Eval()
248 251
 
249 252
 struct SimpleStmt
250
-	*Node
253
+	*common.Node
251 254
 	SendStmt       *SendStmt
252 255
 	Expression     *Expression
253 256
 	IncDecStmt     *IncDecStmt
@@ -263,13 +266,13 @@ struct SimpleStmt
263 266
 		else                         => "\n"
264 267
 
265 268
 struct SendStmt
266
-	*Node
269
+	*common.Node
267 270
 	Left  *Expression
268 271
 	Right *Expression
269 272
 	Eval: string -> @Left.Eval() + "<-" + @Right.Eval()
270 273
 
271 274
 struct IncDecStmt
272
-	*Node
275
+	*common.Node
273 276
 	Expression *Expression
274 277
 	IsInc      bool
275 278
 	Eval: string ->
@@ -278,32 +281,32 @@ struct IncDecStmt
278 281
 		else      => res + "--"
279 282
 
280 283
 struct Assignment
281
-	*Node
284
+	*common.Node
282 285
 	Left  *ExpressionList
283 286
 	Op    string
284 287
 	Right *ExpressionList
285 288
 	Eval: string -> @Left.Eval() + @Op + @Right.Eval()
286 289
 
287 290
 struct ShortVarDecl
288
-	*Node
289
-	Identifiers []string
291
+	*common.Node
292
+	IdentifierList *IdentifierList
290 293
 	Expressions *ExpressionList
291 294
 	// Statement   *Statement
292 295
 	Eval: string ->
293 296
 		res := ""
294
-		res += strings.Join(@Identifiers, ",") + ":="
297
+		if @IdentifierList != nil => res += @IdentifierList.Eval() + ":="
295 298
 		if @Expressions != nil => res += @Expressions.Eval()
296 299
 		// if @Statement   != nil => res += @Statement.Eval()
297 300
 		res
298 301
 
299 302
 struct LabeledStmt
300
-	*Node
303
+	*common.Node
301 304
 	Name      string
302 305
 	Statement *Statement
303 306
 	Eval: string -> @Name + ": " + @Statement.Eval()
304 307
 
305 308
 struct ReturnStmt
306
-	*Node
309
+	*common.Node
307 310
 	Expressions *ExpressionList
308 311
 	Eval: string ->
309 312
 		res := "return "
@@ -311,31 +314,31 @@ struct ReturnStmt
311 314
 		res
312 315
 
313 316
 struct BreakStmt
314
-	*Node
317
+	*common.Node
315 318
 	Name string
316 319
 	Eval: string -> "break " + @Name
317 320
 
318 321
 struct ContinueStmt
319
-	*Node
322
+	*common.Node
320 323
 	Name string
321 324
 	Eval: string -> "continue " + @Name
322 325
 
323 326
 struct GotoStmt
324
-	*Node
327
+	*common.Node
325 328
 	Name string
326 329
 	Eval: string -> "goto " + @Name
327 330
 
328 331
 struct FallthroughStmt
329
-	*Node
332
+	*common.Node
330 333
 	Eval: string -> "fallthrough"
331 334
 
332 335
 struct DeferStmt
333
-	*Node
336
+	*common.Node
334 337
 	Expression *Expression
335 338
 	Eval: string -> "defer " + @Expression.Eval()
336 339
 
337 340
 struct IfStmt
338
-	*Node
341
+	*common.Node
339 342
 	SimpleStmt *SimpleStmt
340 343
 	Expression *Expression
341 344
 	Block      *Block
@@ -360,60 +363,57 @@ struct IfStmt
360 363
 
361 364
 		/* FIXME: Problem with large nested struct decl */
362 365
 		funcLit := &FunctionLit
363
-			Node: NewNodeNoCtx()
366
+			Node: common.NewNodeNoCtx(&FunctionLit{})
364 367
 			Function: &Function
365
-				Node: NewNodeNoCtx()
368
+				Node: common.NewNodeNoCtx(&Function{})
366 369
 				Signature: &Signature
367
-					Node: NewNodeNoCtx()
370
+					Node: common.NewNodeNoCtx(&Signature{})
368 371
 					Parameters: &Parameters
369
-						Node: NewNodeNoCtx()
372
+						Node: common.NewNodeNoCtx(&Parameters{})
370 373
 						List: []*Parameter{}
371 374
 					Result: &Result
372
-						Node: NewNodeNoCtx()
375
+						Node: common.NewNodeNoCtx(&Result{})
373 376
 						Types: []*Type{t}
374 377
 				Block: &Block
375
-					Node: NewNodeNoCtx()
378
+					Node: common.NewNodeNoCtx(&Block{})
376 379
 					Statements: []*Statement
377 380
 						&Statement
378
-							Node: NewNodeNoCtx()
381
+							Node: common.NewNodeNoCtx(&Statement{})
379 382
 							IfStmt: @
380 383
 
381 384
 		primary := &PrimaryExpr
382
-			Node: NewNodeNoCtx()
385
+			Node: common.NewNodeNoCtx(&PrimaryExpr{})
383 386
 			Operand: &Operand
384
-				Node: NewNodeNoCtx()
387
+				Node: common.NewNodeNoCtx(&Operand{})
385 388
 				Literal: &Literal
386
-					Node: NewNodeNoCtx()
389
+					Node: common.NewNodeNoCtx(&Literal{})
387 390
 					FunctionLit: funcLit
388 391
 
389 392
 		unary := &UnaryExpr
390
-			Node: NewNodeNoCtx()
393
+			Node: common.NewNodeNoCtx(&UnaryExpr{})
391 394
 			PrimaryExpr: &PrimaryExpr
392
-				Node: NewNodeNoCtx()
395
+				Node: common.NewNodeNoCtx(&PrimaryExpr{})
393 396
 				PrimaryExpr: primary
394 397
 				SecondaryExpr: &SecondaryExpr
395
-					Node: NewNodeNoCtx()
398
+					Node: common.NewNodeNoCtx(&SecondaryExpr{})
396 399
 					Arguments: &Arguments
397
-						Node: NewNodeNoCtx()
400
+						Node: common.NewNodeNoCtx(&Arguments{})
398 401
 
399 402
 		expr := &Expression
400
-			Node: NewNodeNoCtx()
403
+			Node: common.NewNodeNoCtx(&Expression{})
401 404
 			UnaryExpr: unary
402 405
 
403 406
 		stmt := &Statement
404
-			Node: NewNodeNoCtx()
407
+			Node: common.NewNodeNoCtx(&Statement{})
405 408
 			SimpleStmt: &SimpleStmt
406
-				Node: NewNodeNoCtx()
409
+				Node: common.NewNodeNoCtx(&SimpleStmt{})
407 410
 				Expression: expr
408 411
 
409 412
 		return stmt
410
-		/* TODO: Fixme: without return the parser think this is `@AddReturn() & Statement` */
411
-		/* return &Statement */
412
-		// Node: NewNodeNoCtx()
413 413
 
414 414
 
415 415
 struct SwitchStmt
416
-	*Node
416
+	*common.Node
417 417
 	ExprSwitchStmt *ExprSwitchStmt
418 418
 	TypeSwitchStmt *TypeSwitchStmt
419 419
 	Eval: string ->
@@ -422,7 +422,7 @@ struct SwitchStmt
422 422
 		""
423 423
 
424 424
 struct ExprSwitchStmt
425
-	*Node
425
+	*common.Node
426 426
 	SimpleStmt      *SimpleStmt
427 427
 	Expression      *Expression
428 428
 	ExprCaseClauses []*ExprCaseClause
@@ -436,7 +436,7 @@ struct ExprSwitchStmt
436 436
 		res + "}"
437 437
 
438 438
 struct ExprCaseClause
439
-	*Node
439
+	*common.Node
440 440
 	ExprSwitchCase *ExprSwitchCase
441 441
 	Statements     []*Statement
442 442
 	Eval: string ->
@@ -449,7 +449,7 @@ struct ExprCaseClause
449 449
 		res
450 450
 
451 451
 struct ExprSwitchCase
452
-	*Node
452
+	*common.Node
453 453
 	Expressions *ExpressionList
454 454
 	IsDefault   bool
455 455
 	Eval: string ->
@@ -457,7 +457,7 @@ struct ExprSwitchCase
457 457
 		"case " + @Expressions.Eval()
458 458
 
459 459
 struct TypeSwitchStmt
460
-	*Node
460
+	*common.Node
461 461
 	SimpleStmt       *SimpleStmt
462 462
 	TypeSwitchGuard  *TypeSwitchGuard
463 463
 	TypeCaseClauses  []*TypeCaseClause
@@ -471,7 +471,7 @@ struct TypeSwitchStmt
471 471
 		res + "}"
472 472
 
473 473
 struct TypeSwitchGuard
474
-	*Node
474
+	*common.Node
475 475
 	Name        string
476 476
 	PrimaryExpr *PrimaryExpr
477 477
 	Eval: string ->
@@ -480,7 +480,7 @@ struct TypeSwitchGuard
480 480
 		res + @PrimaryExpr.Eval() + ".(type)"
481 481
 
482 482
 struct TypeCaseClause
483
-	*Node
483
+	*common.Node
484 484
 	TypeSwitchCase *TypeSwitchCase
485 485
 	Statements     []*Statement
486 486
 	Eval: string ->
@@ -492,7 +492,7 @@ struct TypeCaseClause
492 492
 		res
493 493
 
494 494
 struct TypeSwitchCase
495
-	*Node
495
+	*common.Node
496 496
 	Types []*Type
497 497
 	Eval: string ->
498 498
 		res := ""
@@ -504,7 +504,7 @@ struct TypeSwitchCase
504 504
 		res
505 505
 
506 506
 struct SelectStmt
507
-	*Node
507
+	*common.Node
508 508
 	CommClauses []*CommClause
509 509
 	Eval: string ->
510 510
 		res := "select {\n"
@@ -513,7 +513,7 @@ struct SelectStmt
513 513
 		res + "}"
514 514
 
515 515
 struct CommClause
516
-	*Node
516
+	*common.Node
517 517
 	CommCase *CommCase
518 518
 	Block *Block
519 519
 	Eval: string ->
@@ -521,7 +521,7 @@ struct CommClause
521 521
 		@CommCase.Eval() + ":" + block[1:len(block)-1]
522 522
 
523 523
 struct CommCase
524
-	*Node
524
+	*common.Node
525 525
 	SendStmt *SendStmt
526 526
 	RecvStmt *RecvStmt
527 527
 	IsDefault bool
@@ -532,20 +532,20 @@ struct CommCase
532 532
 		""
533 533
 
534 534
 struct RecvStmt
535
-	*Node
535
+	*common.Node
536 536
 	Expressions *ExpressionList
537
-	Identifiers []string
537
+	IdentifierList *IdentifierList
538 538
 	Expression  *Expression
539 539
 	Eval: string ->
540 540
 		res := ""
541 541
 		if @Expressions != nil => res += @Expressions.Eval() + "="
542
-		res += strings.Join(@Identifiers, ",")
543
-		if len(@Identifiers) > 0 => res += ":="
542
+		if @IdentifierList != nil => res += @IdentifierList.Eval()
543
+		if len(res) > 0 => res += ":="
544 544
 		res += @Expression.Eval()
545 545
 		res
546 546
 
547 547
 struct ForStmt
548
-	*Node
548
+	*common.Node
549 549
 	Expression  *Expression
550 550
 	ForClause   *ForClause
551 551
 	RangeClause *RangeClause
@@ -558,7 +558,7 @@ struct ForStmt
558 558
 		res + @Block.Eval()
559 559
 
560 560
 struct ForClause
561
-	*Node
561
+	*common.Node
562 562
 	LeftSimpleStmt  *SimpleStmt
563 563
 	Expression      *Expression
564 564
 	RightSimpleStmt *SimpleStmt
@@ -572,19 +572,19 @@ struct ForClause
572 572
 		res
573 573
 
574 574
 struct RangeClause
575
-	*Node
575
+	*common.Node
576 576
 	// Expressions *ExpressionList
577
-	Identifiers []string
577
+	IdentifierList *IdentifierList
578 578
 	Expression  *Expression
579 579
 	Eval: string ->
580 580
 		res := ""
581 581
 		// if @Expressions != nil => res += @Expressions.Eval()
582
-		res += strings.Join(@Identifiers, ",")
582
+		if @IdentifierList != nil => res += @IdentifierList.Eval()
583 583
 		res += ":= "
584 584
 		res + "range " +  @Expression.Eval()
585 585
 
586 586
 struct GoStmt
587
-	*Node
587
+	*common.Node
588 588
 	Function   *Function
589 589
 	Expression *Expression
590 590
 	Eval: string ->
@@ -593,7 +593,7 @@ struct GoStmt
593 593
 		""
594 594
 
595 595
 struct Type
596
-	*Node
596
+	*common.Node
597 597
 	TypeName string
598 598
 	TypeLit  *TypeLit
599 599
 	Type     *Type
@@ -603,7 +603,7 @@ struct Type
603 603
 		@TypeName
604 604
 
605 605
 struct TypeLit
606
-	*Node
606
+	*common.Node
607 607
 	ArrayType     *ArrayType
608 608
 	StructType    *StructType
609 609
 	PointerType   *PointerType
@@ -624,18 +624,18 @@ struct TypeLit
624 624
 		""
625 625
 
626 626
 struct ArrayType
627
-	*Node
627
+	*common.Node
628 628
 	Length      *Expression
629 629
 	ElementType *Type
630 630
 	Eval: string -> "[" + @Length.Eval() + "]" + @ElementType.Eval()
631 631
 
632 632
 struct PointerType
633
-	*Node
633
+	*common.Node
634 634
 	Type *Type
635 635
 	Eval: string -> "*" + @Type.Eval()
636 636
 
637 637
 struct InterfaceType
638
-	*Node
638
+	*common.Node
639 639
 	Name string
640 640
 	MethodSpecs []*MethodSpec
641 641
 	Eval: string ->
@@ -647,24 +647,24 @@ struct InterfaceType
647 647
 		@Name + " interface {" + res  + "}"
648 648
 
649 649
 struct SliceType
650
-	*Node
650
+	*common.Node
651 651
 	Type *Type
652 652
 	Eval: string -> "[]" + @Type.Eval()
653 653
 
654 654
 struct MapType
655
-	*Node
655
+	*common.Node
656 656
 	InnerType *Type
657 657
 	OuterType *Type
658 658
 	Eval: string -> "map[" + @InnerType.Eval() + "]" + @OuterType.Eval()
659 659
 
660 660
 struct ChannelType
661
-	*Node
661
+	*common.Node
662 662
 	ChannelDecl string
663 663
 	Type        *Type
664 664
 	Eval: string -> @ChannelDecl + " " + @Type.Eval()
665 665
 
666 666
 struct MethodSpec
667
-	*Node
667
+	*common.Node
668 668
 	Name string
669 669
 	Parameters *Parameters
670 670
 	Result     *Result
@@ -677,12 +677,12 @@ struct MethodSpec
677 677
 		res
678 678
 
679 679
 struct FunctionType
680
-	*Node
680
+	*common.Node
681 681
 	Signature *Signature
682 682
 	Eval: string -> "func " + @Signature.Eval()
683 683
 
684 684
 struct Signature
685
-	*Node
685
+	*common.Node
686 686
 	TemplateSpec *TemplateSpec
687 687
 	Parameters   *Parameters
688 688
 	Result       *Result
@@ -693,11 +693,11 @@ struct Signature
693 693
 		res
694 694
 
695 695
 struct TemplateSpec
696
-	*Node
696
+	*common.Node
697 697
 	Result *Result
698 698
 
699 699
 struct Result
700
-	*Node
700
+	*common.Node
701 701
 	Types []*Type
702 702
 	Eval: string ->
703 703
 		res := "("
@@ -707,22 +707,18 @@ struct Result
707 707
 		res
708 708
 
709 709
 struct Parameter
710
-	*Node
711
-	Names      []string
710
+	*common.Node
711
+	IdentifierList *IdentifierList
712 712
 	Type       *Type
713 713
 	IsVariadic bool
714 714
 	Eval: string ->
715 715
 		res := ""
716
-		for _, spec in @Names
717
-			res += spec + ","
718
-		if len(@Names) > 0
719
-			res = res[:len(res)-1]
720
-		res += " "
716
+		if @IdentifierList != nil => res += @IdentifierList.Eval() + " "
721 717
 		if @IsVariadic => res += "..."
722 718
 		res + @Type.Eval()
723 719
 
724 720
 struct Operand
725
-	*Node
721
+	*common.Node
726 722
 	Literal     *Literal
727 723
 	OperandName *OperandName
728 724
 	MethodExpr  *MethodExpr
@@ -735,7 +731,7 @@ struct Operand
735 731
 		""
736 732
 
737 733
 struct Literal
738
-	*Node
734
+	*common.Node
739 735
 	Basic     string
740 736
 	Composite *CompositeLit
741 737
 	FunctionLit     *FunctionLit
@@ -745,19 +741,19 @@ struct Literal
745 741
 		@Basic
746 742
 
747 743
 struct OperandName
748
-	*Node
744
+	*common.Node
749 745
 	Name string
750 746
 	Eval: string -> @Name
751 747
 
752 748
 struct CompositeLit
753
-	*Node
749
+	*common.Node
754 750
 	LiteralType  *LiteralType
755 751
 	TemplateSpec *TemplateSpec
756 752
 	LiteralValue *LiteralValue
757 753
 	Eval: string -> @LiteralType.Eval() + @LiteralValue.Eval()
758 754
 
759 755
 struct LiteralType
760
-	*Node
756
+	*common.Node
761 757
 	Struct   *StructType
762 758
 	Array    *ArrayType
763 759
 	Element  *Type
@@ -773,7 +769,7 @@ struct LiteralType
773 769
 		@Type
774 770
 
775 771
 struct LiteralValue
776
-	*Node
772
+	*common.Node
777 773
 	Elements []*KeyedElement
778 774
 	Eval: string ->
779 775
 		res := "{"
@@ -784,7 +780,7 @@ struct LiteralValue
784 780
 		res + "}"
785 781
 
786 782
 struct KeyedElement
787
-	*Node
783
+	*common.Node
788 784
 	Key     *Key
789 785
 	Element *Element
790 786
 	Eval: string ->
@@ -793,7 +789,7 @@ struct KeyedElement
793 789
 		res + @Element.Eval()
794 790
 
795 791
 struct Key
796
-	*Node
792
+	*common.Node
797 793
 	Name         string
798 794
 	Expression   *Expression
799 795
 	LiteralValue *LiteralValue
@@ -803,7 +799,7 @@ struct Key
803 799
 		@Name
804 800
 
805 801
 struct Element
806
-	*Node
802
+	*common.Node
807 803
 	Expression   *Expression
808 804
 	LiteralValue *LiteralValue
809 805
 	Eval: string ->
@@ -812,7 +808,7 @@ struct Element
812 808
 		""
813 809
 
814 810
 struct StructType
815
-	*Node
811
+	*common.Node
816 812
 	Name string
817 813
 	TemplateSpec *TemplateSpec
818 814
 	Fields []*FieldDecl
@@ -829,27 +825,33 @@ struct StructType
829 825
 		res + "}" + methods
830 826
 
831 827
 struct FieldDecl
832
-	*Node
833
-	Identifiers []string
828
+	*common.Node
829
+	IdentifierList *IdentifierList
834 830
 	Type        *Type
835 831
 	Anonymous   *AnonymousField
836 832
 	Tag         string
837 833
 	InlineStructMethod *InlineStructMethod
838 834
 	Eval: string ->
839 835
 		if @InlineStructMethod != nil => return @InlineStructMethod.Eval()
840
-		res := strings.Join(@Identifiers, ",")
836
+		res := ""
837
+		if @IdentifierList != nil => res += @IdentifierList.Eval()
841 838
 		if @Type != nil => res += " " + @Type.Eval()
842 839
 		if @Anonymous != nil => res += @Anonymous.Eval()
843 840
 		res + " " + @Tag
844 841
 
842
+struct IdentifierList
843
+	*common.Node
844
+	List []string
845
+	Eval: string -> strings.Join(@List, ",")
846
+
845 847
 struct InlineStructMethod
846
-	*Node
848
+	*common.Node
847 849
 	IsPointerReceiver bool
848 850
 	FunctionDecl      *FunctionDecl
849 851
 	Eval: string -> @FunctionDecl.Eval()[4:]
850 852
 
851 853
 struct AnonymousField
852
-	*Node
854
+	*common.Node
853 855
 	IsPointerReceiver bool
854 856
 	Type              string
855 857
 	Eval: string ->
@@ -858,12 +860,12 @@ struct AnonymousField
858 860
 		res
859 861
 
860 862
 struct FunctionLit
861
-	*Node
863
+	*common.Node
862 864
 	Function *Function
863 865
 	Eval: string -> "func " + @Function.Eval()
864 866
 
865 867
 struct PrimaryExpr
866
-	*Node
868
+	*common.Node
867 869
 	Operand       *Operand
868 870
 	Conversion    *Conversion
869 871
 	PrimaryExpr   *PrimaryExpr
@@ -875,7 +877,7 @@ struct PrimaryExpr
875 877
 		""
876 878
 
877 879
 struct SecondaryExpr
878
-	*Node
880
+	*common.Node
879 881
 	Selector      string
880 882
 	Index         *Index
881 883
 	Slice         *Slice
@@ -890,12 +892,12 @@ struct SecondaryExpr
890 892
 		""
891 893
 
892 894
 struct Index
893
-	*Node
895
+	*common.Node
894 896
 	Expression *Expression
895 897
 	Eval: string -> "[" + @Expression.Eval() + "]"
896 898
 
897 899
 struct Slice
898
-	*Node
900
+	*common.Node
899 901
 	LeftExpr   *Expression
900 902
 	MiddleExpr *Expression
901 903
 	RightExpr  *Expression
@@ -908,12 +910,12 @@ struct Slice
908 910
 		res + "]"
909 911
 
910 912
 struct TypeAssertion
911
-	*Node
913
+	*common.Node
912 914
 	Type *Type
913 915
 	Eval: string -> ".(" + @Type.Eval() + ")"
914 916
 
915 917
 struct Arguments
916
-	*Node
918
+	*common.Node
917 919
 	TemplateSpec *TemplateSpec
918 920
 	Expressions  *ExpressionList
919 921
 	Type         *Type
@@ -929,13 +931,13 @@ struct Arguments
929 931
 		res + ")"
930 932
 
931 933
 struct MethodExpr
932
-	*Node
934
+	*common.Node
933 935
 	ReceiverType *ReceiverType
934 936
 	Name         string
935 937
 	Eval: string -> @ReceiverType.Eval() + "." + @Name
936 938
 
937 939
 struct ReceiverType
938
-	*Node
940
+	*common.Node
939 941
 	Type         string
940 942
 	IsPointer    bool
941 943
 	ReceiverType *ReceiverType
@@ -947,7 +949,7 @@ struct ReceiverType
947 949
 		res
948 950
 
949 951
 struct Expression
950
-	*Node
952
+	*common.Node
951 953
 	UnaryExpr       *UnaryExpr
952 954
 	// FunctionLit     *FunctionLit
953 955
 	LeftExpression  *Expression
@@ -959,7 +961,7 @@ struct Expression
959 961
 		@LeftExpression.Eval() + @Op + @RightExpression.Eval()
960 962
 
961 963
 struct UnaryExpr
962
-	*Node
964
+	*common.Node
963 965
 	PrimaryExpr *PrimaryExpr
964 966
 	Op          string
965 967
 	UnaryExpr   *UnaryExpr
@@ -968,13 +970,13 @@ struct UnaryExpr
968 970
 		@Op + @UnaryExpr.Eval()
969 971
 
970 972
 struct Conversion
971
-	*Node
973
+	*common.Node
972 974
 	Type       *Type
973 975
 	Expression *Expression
974 976
 	Eval: string -> @Type.Eval() + "(" + @Expression.Eval() + ")"
975 977
 
976 978
 struct Interpret
977
-	*Node
979
+	*common.Node
978 980
 	Statement *Statement
979 981
 	TopLevel  *TopLevel
980 982
 	Eval: string ->

+ 0
- 38
lib/ast/node.go View File

@@ -1,38 +0,0 @@
1
-package ast
2
-
3
-import (
4
-	"github.com/antlr/antlr4/runtime/Go/antlr"
5
-)
6
-
7
-type INode interface {
8
-	Eval() string
9
-	Text() string
10
-	SetParent(n INode)
11
-	GetParent() INode
12
-}
13
-type Node struct {
14
-	Text_    string
15
-	Children []INode
16
-	parent   INode
17
-}
18
-
19
-func (this Node) Eval() string {
20
-	return ""
21
-}
22
-func (this Node) Text() string {
23
-	return this.Text_
24
-}
25
-func (this *Node) SetParent(n INode) {
26
-	if this.parent == nil {
27
-		this.parent = n
28
-	}
29
-}
30
-func (this *Node) GetParent() INode {
31
-	return this.parent
32
-}
33
-func NewNode(ctx antlr.RuleContext) *Node {
34
-	return &Node{Text_: ctx.GetText()}
35
-}
36
-func NewNodeNoCtx() *Node {
37
-	return &Node{}
38
-}

+ 0
- 27
lib/ast/node.og View File

@@ -1,27 +0,0 @@
1
-!ast
2
-
3
-import
4
-	"github.com/antlr/antlr4/runtime/Go/antlr"
5
-
6
-interface INode
7
-	Eval: string
8
-	Text: string
9
-	SetParent(n INode)
10
-	GetParent: INode
11
-
12
-struct Node
13
-	Text_     string
14
-	Children []INode
15
-	parent   INode
16
-
17
-	Eval: string ->  ""
18
-	Text: string -> @Text_
19
-	*SetParent(n INode) -> if @parent == nil => @parent = n;
20
-	*GetParent: INode -> @parent
21
-
22
-NewNode(ctx antlr.RuleContext): *Node ->
23
-	&Node
24
-		Text_: ctx.GetText()
25
-
26
-NewNodeNoCtx(): *Node ->
27
-	&Node{}

+ 1
- 1
lib/ast/walker/desugar.go View File

@@ -14,7 +14,7 @@ func (this *Desugar) Run(files []*common.File) {
14 14
 		RunTemplateParse(file.Ast, this.Templates)
15 15
 	}
16 16
 	for _, file := range files {
17
-		RunTemplateUsage(file.Ast, this.Templates)
17
+		RunTemplateUsage(file, this.Templates)
18 18
 		RunTemplateGenerator(file.Ast, this.Templates)
19 19
 		this.Templates.ResetUsedFor()
20 20
 	}

+ 1
- 1
lib/ast/walker/desugar.og View File

@@ -12,7 +12,7 @@ struct Desugar
12 12
 			RunTemplateParse(file.Ast, @Templates)
13 13
 
14 14
 		for _, file in files
15
-			RunTemplateUsage(file.Ast, @Templates)
15
+			RunTemplateUsage(file, @Templates)
16 16
 			RunTemplateGenerator(file.Ast, @Templates)
17 17
 			@Templates.ResetUsedFor()
18 18
 

+ 3
- 3
lib/ast/walker/gob_register.go View File

@@ -2,17 +2,17 @@ package walker
2 2
 
3 3
 import (
4 4
 	"encoding/gob"
5
-	"github.com/champii/og/lib/ast"
5
+	"github.com/champii/og/lib/common"
6 6
 )
7 7
 
8 8
 type GobRegister struct {
9 9
 	AstWalker
10 10
 }
11 11
 
12
-func (this *GobRegister) Each(n ast.INode) {
12
+func (this *GobRegister) Each(n common.INode) {
13 13
 	gob.Register(n)
14 14
 }
15
-func RunGobRegister(tree ast.INode) {
15
+func RunGobRegister(tree common.INode) {
16 16
 	p := GobRegister{}
17 17
 	gob.Register(tree)
18 18
 	p.type_ = &p

+ 3
- 3
lib/ast/walker/gob_register.og View File

@@ -2,13 +2,13 @@
2 2
 
3 3
 import
4 4
 	"encoding/gob"
5
-	"github.com/champii/og/lib/ast"
5
+	"github.com/champii/og/lib/common"
6 6
 
7 7
 struct GobRegister
8 8
 	AstWalker
9
-	*Each(n ast.INode) -> gob.Register(n)
9
+	*Each(n common.INode) -> gob.Register(n)
10 10
 
11
-RunGobRegister(tree ast.INode) ->
11
+RunGobRegister(tree common.INode) ->
12 12
 	p := GobRegister{}
13 13
 	gob.Register(tree)
14 14
 	p.type_ = &p

+ 49
- 7
lib/ast/walker/print.go View File

@@ -2,29 +2,71 @@ package walker
2 2
 
3 3
 import (
4 4
 	"fmt"
5
-	"github.com/champii/og/lib/ast"
5
+	"github.com/champii/og/lib/common"
6
+	"github.com/fatih/color"
6 7
 	"reflect"
8
+	"strconv"
7 9
 	"strings"
8 10
 )
9 11
 
12
+var (
13
+	magenta = color.New(color.Bold, color.FgHiMagenta).SprintFunc()
14
+)
15
+var (
16
+	yellow = color.New(color.FgHiYellow).SprintfFunc()
17
+)
18
+var (
19
+	green = color.New(color.FgHiGreen).SprintfFunc()
20
+)
21
+var (
22
+	cyan = color.New(color.FgCyan).SprintFunc()
23
+)
24
+
10 25
 type Printer struct {
11 26
 	AstWalker
27
+	Simple bool
12 28
 	indent int
13 29
 }
14 30
 
15
-func (this *Printer) Before(n ast.INode) {
31
+func (this *Printer) Before(n common.INode) {
16 32
 	this.indent++
17 33
 }
18
-func (this *Printer) After(n ast.INode) {
34
+func (this *Printer) After(n common.INode) {
19 35
 	this.indent--
20 36
 }
21
-func (this *Printer) Each(n ast.INode) ast.INode {
37
+func (this Printer) getTerminalText(n common.INode) string {
38
+	txt := ""
39
+	struc := reflect.ValueOf(n).Elem()
40
+	for i := 0; i < struc.NumField(); i++ {
41
+		if struc.Field(i).Kind() == reflect.String {
42
+			txt = struc.Field(i).String()
43
+		}
44
+		if struc.Field(i).Kind() == reflect.Slice {
45
+			if struc.Field(i).Type().Elem().Kind() == reflect.String {
46
+				txt = strings.Join(struc.Field(i).Interface().([]string), ",")
47
+			}
48
+		}
49
+	}
50
+	return txt
51
+}
52
+func (this *Printer) Each(n common.INode) common.INode {
22 53
 	name := reflect.TypeOf(n).String()[5:]
23
-	fmt.Printf("%s\n", strings.Repeat(" ", this.indent)+name)
54
+	txt := this.getTerminalText(n)
55
+	if this.Simple && len(txt) == 0 {
56
+		return n
57
+	}
58
+	line := fmt.Sprintf("(%s:%s):", yellow("%d", n.Line()), yellow("%d", n.Col()))
59
+	line += strings.Repeat(" ", 8-len(strconv.Itoa(n.Line())+strconv.Itoa(n.Col())))
60
+	if len(txt) > 0 {
61
+		name = green(name)
62
+	} else {
63
+		name = cyan(name)
64
+	}
65
+	fmt.Printf("%s %s %s\n", line, strings.Repeat(" ", this.indent)+name, magenta(txt))
24 66
 	return n
25 67
 }
26
-func Print(ast ast.INode) {
27
-	p := Printer{}
68
+func Print(ast common.INode, simple bool) {
69
+	p := Printer{Simple: simple}
28 70
 	p.type_ = &p
29 71
 	p.Walk(ast)
30 72
 }

+ 42
- 7
lib/ast/walker/print.og View File

@@ -4,23 +4,58 @@ import
4 4
 	fmt
5 5
 	reflect
6 6
 	strings
7
-	"github.com/champii/og/lib/ast"
7
+	strconv
8
+	"github.com/fatih/color"
9
+	"github.com/champii/og/lib/common"
10
+
11
+var magenta = color.New(color.Bold, color.FgHiMagenta).SprintFunc()
12
+var yellow = color.New(color.FgHiYellow).SprintfFunc()
13
+var green = color.New(color.FgHiGreen).SprintfFunc()
14
+var cyan = color.New(color.FgCyan).SprintFunc()
8 15
 
9 16
 struct Printer
10 17
 	AstWalker
18
+	Simple bool
11 19
 	indent int
12 20
 
13
-	*Before(n ast.INode) -> @indent++
14
-	*After(n ast.INode) -> @indent--
21
+	*Before(n common.INode) -> @indent++
22
+	*After(n common.INode) -> @indent--
23
+
24
+	getTerminalText(n common.INode): string ->
25
+		txt := ""
26
+
27
+		struc := reflect.ValueOf(n).Elem()
28
+		for i := 0; i < struc.NumField(); i++
29
+			if struc.Field(i).Kind() == reflect.String
30
+				txt = struc.Field(i).String()
31
+			if struc.Field(i).Kind() == reflect.Slice
32
+				if struc.Field(i).Type().Elem().Kind() == reflect.String
33
+					txt = strings.Join(struc.Field(i).Interface().([]string), ",")
34
+
35
+		txt
15 36
 
16
-	*Each(n ast.INode): ast.INode ->
37
+	*Each(n common.INode): common.INode ->
17 38
 		name := reflect.TypeOf(n).String()[5:]
39
+		txt := @getTerminalText(n)
40
+
41
+		if @Simple && len(txt) == 0
42
+			return n
43
+
44
+		line := fmt.Sprintf("(%s:%s):", yellow("%d", n.Line()), yellow("%d", n.Col()))
45
+		line += strings.Repeat(" ", 8 - len(strconv.Itoa(n.Line()) + strconv.Itoa(n.Col())))
46
+
47
+		if len(txt) > 0
48
+			name = green(name)
49
+		else
50
+			name = cyan(name)
51
+
52
+		fmt.Printf("%s %s %s\n", line, strings.Repeat(" ", @indent) + name, magenta(txt))
18 53
 
19
-		fmt.Printf("%s\n", strings.Repeat(" ", @indent) + name)
20 54
 		n
21 55
 
22
-Print(ast ast.INode) ->
23
-	p := Printer{}
56
+Print(ast common.INode, simple bool) ->
57
+	p := Printer
58
+		Simple: simple
24 59
 
25 60
 	p.type_ = &p
26 61
 

+ 5
- 4
lib/ast/walker/returnable.go View File

@@ -2,14 +2,15 @@ package walker
2 2
 
3 3
 import (
4 4
 	"github.com/champii/og/lib/ast"
5
+	"github.com/champii/og/lib/common"
5 6
 )
6 7
 
7 8
 type Returnable struct {
8 9
 	AstWalker
9
-	Root ast.INode
10
+	Root common.INode
10 11
 }
11 12
 
12
-func (this *Returnable) VarDecl(n ast.INode) ast.INode {
13
+func (this *Returnable) VarDecl(n common.INode) common.INode {
13 14
 	varDecl := n.(*ast.VarDecl)
14 15
 	for _, varSpec := range varDecl.VarSpecs {
15 16
 		statement := varSpec.Statement
@@ -28,7 +29,7 @@ func (this *Returnable) VarDecl(n ast.INode) ast.INode {
28 29
 	}
29 30
 	return varDecl
30 31
 }
31
-func (this *Returnable) Function(n ast.INode) ast.INode {
32
+func (this *Returnable) Function(n common.INode) common.INode {
32 33
 	function := n.(*ast.Function)
33 34
 	sig := function.Signature
34 35
 	if sig == nil {
@@ -52,7 +53,7 @@ func (this *Returnable) Function(n ast.INode) ast.INode {
52 53
 	}
53 54
 	return n
54 55
 }
55
-func RunReturnable(tree ast.INode) ast.INode {
56
+func RunReturnable(tree common.INode) common.INode {
56 57
 	returnable := Returnable{Root: tree}
57 58
 	returnable.type_ = &returnable
58 59
 	return returnable.Walk(tree)

+ 5
- 4
lib/ast/walker/returnable.og View File

@@ -2,12 +2,13 @@
2 2
 
3 3
 import
4 4
 	"github.com/champii/og/lib/ast"
5
+	"github.com/champii/og/lib/common"
5 6
 
6 7
 struct Returnable
7 8
 	AstWalker
8
-	Root      ast.INode
9
+	Root      common.INode
9 10
 
10
-	*VarDecl(n ast.INode): ast.INode ->
11
+	*VarDecl(n common.INode): common.INode ->
11 12
 		varDecl := n.(*ast.VarDecl)
12 13
 		for _, varSpec in varDecl.VarSpecs
13 14
 			statement := varSpec.Statement
@@ -27,7 +28,7 @@ struct Returnable
27 28
 
28 29
 		varDecl
29 30
 
30
-	*Function(n ast.INode): ast.INode ->
31
+	*Function(n common.INode): common.INode ->
31 32
 		function := n.(*ast.Function)
32 33
 		sig := function.Signature
33 34
 
@@ -51,7 +52,7 @@ struct Returnable
51 52
 
52 53
 		n
53 54
 
54
-RunReturnable(tree ast.INode): ast.INode ->
55
+RunReturnable(tree common.INode): common.INode ->
55 56
 	returnable := Returnable
56 57
 		Root:      tree
57 58
 

+ 3
- 3
lib/ast/walker/template.go View File

@@ -1,7 +1,7 @@
1 1
 package walker
2 2
 
3 3
 import (
4
-	"github.com/champii/og/lib/ast"
4
+	"github.com/champii/og/lib/common"
5 5
 	"reflect"
6 6
 	"strings"
7 7
 )
@@ -11,7 +11,7 @@ type Template struct {
11 11
 	Types        []string
12 12
 	UsedFor      [][]string
13 13
 	GeneratedFor map[string][]string
14
-	Node         ast.INode
14
+	Node         common.INode
15 15
 }
16 16
 
17 17
 func (this Template) contains(arr []string, str string) bool {
@@ -42,7 +42,7 @@ func (this *Template) AddUsedFor(types []string) {
42 42
 	}
43 43
 	this.UsedFor = append(this.UsedFor, types)
44 44
 }
45
-func NewTemplate(name string, types []string, node ast.INode) *Template {
45
+func NewTemplate(name string, types []string, node common.INode) *Template {
46 46
 	return &Template{
47 47
 		Name:         name,
48 48
 		Types:        types,

+ 3
- 3
lib/ast/walker/template.og View File

@@ -3,14 +3,14 @@
3 3
 import
4 4
 	strings
5 5
 	reflect
6
-	"github.com/champii/og/lib/ast"
6
+	"github.com/champii/og/lib/common"
7 7
 
8 8
 struct Template
9 9
 	Name         string
10 10
 	Types        []string
11 11
 	UsedFor      [][]string
12 12
 	GeneratedFor map[string][]string // Join(UsedFor[i], ",") -> []PackageName
13
-	Node         ast.INode
13
+	Node         common.INode
14 14
 
15 15
 	contains(arr []string, str string): bool ->
16 16
 		for _, item in arr
@@ -41,7 +41,7 @@ struct Template
41 41
 
42 42
 		@UsedFor = append(@UsedFor, types)
43 43
 
44
-NewTemplate(name string, types []string, node ast.INode): *Template ->
44
+NewTemplate(name string, types []string, node common.INode): *Template ->
45 45
 	&Template
46 46
 		Name: name
47 47
 		Types: types

+ 3
- 2
lib/ast/walker/template_apply.go View File

@@ -2,6 +2,7 @@ package walker
2 2
 
3 3
 import (
4 4
 	"github.com/champii/og/lib/ast"
5
+	"github.com/champii/og/lib/common"
5 6
 )
6 7
 
7 8
 type TemplateApply struct {
@@ -10,7 +11,7 @@ type TemplateApply struct {
10 11
 	typeDest []string
11 12
 }
12 13
 
13
-func (this *TemplateApply) Type(n ast.INode) ast.INode {
14
+func (this *TemplateApply) Type(n common.INode) common.INode {
14 15
 	t := n.(*ast.Type)
15 16
 	tName := t.Eval()
16 17
 	for i, ty := range this.typeSrc {
@@ -22,7 +23,7 @@ func (this *TemplateApply) Type(n ast.INode) ast.INode {
22 23
 	}
23 24
 	return n
24 25
 }
25
-func RunTemplateApply(tree ast.INode, typeSrc []string, typeDest []string) ast.INode {
26
+func RunTemplateApply(tree common.INode, typeSrc []string, typeDest []string) common.INode {
26 27
 	templateApply := TemplateApply{
27 28
 		typeSrc:  typeSrc,
28 29
 		typeDest: typeDest,

+ 3
- 2
lib/ast/walker/template_apply.og View File

@@ -2,13 +2,14 @@
2 2
 
3 3
 import
4 4
 	"github.com/champii/og/lib/ast"
5
+	"github.com/champii/og/lib/common"
5 6
 
6 7
 struct TemplateApply
7 8
 	AstWalker
8 9
 	typeSrc []string
9 10
 	typeDest []string
10 11
 
11
-	*Type(n ast.INode): ast.INode ->
12
+	*Type(n common.INode): common.INode ->
12 13
 		t := n.(*ast.Type)
13 14
 		tName := t.Eval()
14 15
 		for i, ty in @typeSrc
@@ -18,7 +19,7 @@ struct TemplateApply
18 19
 				t.Type = nil
19 20
 		n
20 21
 
21
-RunTemplateApply(tree ast.INode, typeSrc []string, typeDest []string): ast.INode ->
22
+RunTemplateApply(tree common.INode, typeSrc []string, typeDest []string): common.INode ->
22 23
 	templateApply := TemplateApply
23 24
 		typeSrc:  typeSrc
24 25
 		typeDest:  typeDest

+ 3
- 2
lib/ast/walker/template_generator.go View File

@@ -5,11 +5,12 @@ import (
5 5
 	"encoding/gob"
6 6
 	"fmt"
7 7
 	"github.com/champii/og/lib/ast"
8
+	"github.com/champii/og/lib/common"
8 9
 	"strings"
9 10
 )
10 11
 
11 12
 type TemplateGenerator struct {
12
-	Root ast.INode
13
+	Root common.INode
13 14
 }
14 15
 
15 16
 func (this *TemplateGenerator) GenerateStruct(template *Template) {
@@ -92,7 +93,7 @@ func (this *TemplateGenerator) GenerateGenerics(templates *Templates) {
92 93
 		}
93 94
 	}
94 95
 }
95
-func RunTemplateGenerator(tree ast.INode, templates *Templates) {
96
+func RunTemplateGenerator(tree common.INode, templates *Templates) {
96 97
 	templateGenerator := TemplateGenerator{Root: tree}
97 98
 	templateGenerator.GenerateGenerics(templates)
98 99
 }

+ 3
- 2
lib/ast/walker/template_generator.og View File

@@ -6,9 +6,10 @@ import
6 6
 	strings
7 7
 	"encoding/gob"
8 8
 	"github.com/champii/og/lib/ast"
9
+	"github.com/champii/og/lib/common"
9 10
 
10 11
 struct TemplateGenerator
11
-	Root ast.INode
12
+	Root common.INode
12 13
 
13 14
 	*GenerateStruct(template *Template) ->
14 15
 		source := @Root.(*ast.SourceFile)
@@ -89,7 +90,7 @@ struct TemplateGenerator
89 90
 				*ast.FunctionDecl => @GenerateTopFns(template);
90 91
 				*ast.StructType   => @GenerateStruct(template)
91 92
 
92
-RunTemplateGenerator(tree ast.INode, templates *Templates) ->
93
+RunTemplateGenerator(tree common.INode, templates *Templates) ->
93 94
 	templateGenerator := TemplateGenerator
94 95
 		Root: tree
95 96
 

+ 5
- 4
lib/ast/walker/template_parse.go View File

@@ -2,15 +2,16 @@ package walker
2 2
 
3 3
 import (
4 4
 	"github.com/champii/og/lib/ast"
5
+	"github.com/champii/og/lib/common"
5 6
 )
6 7
 
7 8
 type TemplateParse struct {
8 9
 	AstWalker
9
-	Root      ast.INode
10
+	Root      common.INode
10 11
 	Templates *Templates
11 12
 }
12 13
 
13
-func (this *TemplateParse) StructType(n ast.INode) ast.INode {
14
+func (this *TemplateParse) StructType(n common.INode) common.INode {
14 15
 	structType := n.(*ast.StructType)
15 16
 	if structType.TemplateSpec != nil {
16 17
 		types := []string{}
@@ -21,7 +22,7 @@ func (this *TemplateParse) StructType(n ast.INode) ast.INode {
21 22
 	}
22 23
 	return n
23 24
 }
24
-func (this *TemplateParse) Signature(n ast.INode) ast.INode {
25
+func (this *TemplateParse) Signature(n common.INode) common.INode {
25 26
 	sig := n.(*ast.Signature)
26 27
 	if sig.TemplateSpec != nil {
27 28
 		if f, ok := sig.GetParent().(*ast.Function); ok {
@@ -35,7 +36,7 @@ func (this *TemplateParse) Signature(n ast.INode) ast.INode {
35 36
 	}
36 37
 	return n
37 38
 }
38
-func RunTemplateParse(tree ast.INode, templates *Templates) {
39
+func RunTemplateParse(tree common.INode, templates *Templates) {
39 40
 	templateParse := TemplateParse{
40 41
 		Root:      tree,
41 42
 		Templates: templates,

+ 5
- 4
lib/ast/walker/template_parse.og View File

@@ -2,13 +2,14 @@
2 2
 
3 3
 import
4 4
 	"github.com/champii/og/lib/ast"
5
+	"github.com/champii/og/lib/common"
5 6
 
6 7
 struct TemplateParse
7 8
 	AstWalker
8
-	Root      ast.INode
9
+	Root      common.INode
9 10
 	Templates *Templates
10 11
 
11
-	*StructType(n ast.INode): ast.INode ->
12
+	*StructType(n common.INode): common.INode ->
12 13
 		structType := n.(*ast.StructType)
13 14
 		if structType.TemplateSpec != nil
14 15
 			types := []string{}
@@ -18,7 +19,7 @@ struct TemplateParse
18 19
 			@Templates.Add(structType.Name, NewTemplate(structType.Name, types, structType))
19 20
 		n
20 21
 
21
-	*Signature(n ast.INode): ast.INode ->
22
+	*Signature(n common.INode): common.INode ->
22 23
 		sig := n.(*ast.Signature)
23 24
 		if sig.TemplateSpec != nil
24 25
 
@@ -32,7 +33,7 @@ struct TemplateParse
32 33
 				@Templates.Add(fDecl.Name, NewTemplate(fDecl.Name, types, fDecl))
33 34
 		n
34 35
 
35
-RunTemplateParse(tree ast.INode, templates *Templates) ->
36
+RunTemplateParse(tree common.INode, templates *Templates) ->
36 37
 	templateParse := TemplateParse
37 38
 		Root:      tree
38 39
 		Templates: templates

+ 17
- 7
lib/ast/walker/template_usage.go View File

@@ -2,26 +2,35 @@ package walker
2 2
 
3 3
 import (
4 4
 	"github.com/champii/og/lib/ast"
5
+	"github.com/champii/og/lib/common"
6
+	"os"
5 7
 	"strings"
6 8
 )
7 9
 
8 10
 type TemplateUsage struct {
9 11
 	AstWalker
10
-	Root      ast.INode
12
+	File      *common.File
13
+	Root      common.INode
11 14
 	Templates *Templates
12 15
 }
13 16
 
14
-func (this *TemplateUsage) computeTypes(callee ast.INode, templateSpec *ast.TemplateSpec) string {
17
+func (this *TemplateUsage) computeTypes(callee common.INode, templateSpec *ast.TemplateSpec) string {
15 18
 	calleeName := callee.Eval()
16 19
 	types := []string{}
17 20
 	for _, t := range templateSpec.Result.Types {
18 21
 		types = append(types, t.Eval())
19 22
 	}
20 23
 	template := this.Templates.Get(calleeName)
24
+	if template == nil {
25
+		err := this.File.Error(callee.Line(), callee.Col(), "Unknown template name", calleeName)
26
+		common.Print.Error(err)
27
+		os.Exit(1)
28
+		return calleeName
29
+	}
21 30
 	template.AddUsedFor(types)
22 31
 	return calleeName + strings.Join(types, "")
23 32
 }
24
-func (this *TemplateUsage) Arguments(n ast.INode) ast.INode {
33
+func (this *TemplateUsage) Arguments(n common.INode) common.INode {
25 34
 	args := n.(*ast.Arguments)
26 35
 	if args.TemplateSpec != nil {
27 36
 		callee := args.GetParent().(*ast.SecondaryExpr).GetParent().(*ast.PrimaryExpr).PrimaryExpr.Operand
@@ -29,7 +38,7 @@ func (this *TemplateUsage) Arguments(n ast.INode) ast.INode {
29 38
 	}
30 39
 	return n
31 40
 }
32
-func (this *TemplateUsage) CompositeLit(n ast.INode) ast.INode {
41
+func (this *TemplateUsage) CompositeLit(n common.INode) common.INode {
33 42
 	composite := n.(*ast.CompositeLit)
34 43
 	if composite.TemplateSpec != nil {
35 44
 		callee := composite.LiteralType
@@ -37,11 +46,12 @@ func (this *TemplateUsage) CompositeLit(n ast.INode) ast.INode {
37 46
 	}
38 47
 	return n
39 48
 }
40
-func RunTemplateUsage(tree ast.INode, templates *Templates) {
49
+func RunTemplateUsage(file *common.File, templates *Templates) {
41 50
 	templateUsage := TemplateUsage{
42
-		Root:      tree,
51
+		Root:      file.Ast,
52
+		File:      file,
43 53
 		Templates: templates,
44 54
 	}
45 55
 	templateUsage.type_ = &templateUsage
46
-	templateUsage.Walk(tree)
56
+	templateUsage.Walk(file.Ast)
47 57
 }

+ 17
- 7
lib/ast/walker/template_usage.og View File

@@ -1,15 +1,18 @@
1 1
 !walker
2 2
 
3 3
 import
4
+	os
4 5
 	strings
5 6
 	"github.com/champii/og/lib/ast"
7
+	"github.com/champii/og/lib/common"
6 8
 
7 9
 struct TemplateUsage
8 10
 	AstWalker
9
-	Root      ast.INode
11
+	File      *common.File
12
+	Root      common.INode
10 13
 	Templates *Templates
11 14
 
12
-	*computeTypes(callee ast.INode, templateSpec *ast.TemplateSpec): string ->
15
+	*computeTypes(callee common.INode, templateSpec *ast.TemplateSpec): string ->
13 16
 		calleeName := callee.Eval()
14 17
 
15 18
 		types := []string{}
@@ -19,11 +22,17 @@ struct TemplateUsage
19 22
 
20 23
 		template := @Templates.Get(calleeName)
21 24
 
25
+		if template == nil
26
+			err := @File.Error(callee.Line(), callee.Col(), "Unknown template name", calleeName)
27
+			common.Print.Error(err)
28
+			os.Exit(1)
29
+			return calleeName
30
+
22 31
 		template.AddUsedFor(types)
23 32
 
24 33
 		calleeName + strings.Join(types, "")
25 34
 
26
-	*Arguments(n ast.INode): ast.INode ->
35
+	*Arguments(n common.INode): common.INode ->
27 36
 		args := n.(*ast.Arguments)
28 37
 
29 38
 		if args.TemplateSpec != nil
@@ -32,7 +41,7 @@ struct TemplateUsage
32 41
 
33 42
 		n
34 43
 
35
-	*CompositeLit(n ast.INode): ast.INode ->
44
+	*CompositeLit(n common.INode): common.INode ->
36 45
 		composite := n.(*ast.CompositeLit)
37 46
 
38 47
 		if composite.TemplateSpec != nil
@@ -41,11 +50,12 @@ struct TemplateUsage
41 50
 
42 51
 		n
43 52
 
44
-RunTemplateUsage(tree ast.INode, templates *Templates) ->
53
+RunTemplateUsage(file *common.File, templates *Templates) ->
45 54
 	templateUsage := TemplateUsage
46
-		Root: tree
55
+		Root: file.Ast
56
+		File: file
47 57
 		Templates: templates
48 58
 
49 59
 	templateUsage.type_ = &templateUsage
50 60
 
51
-	templateUsage.Walk(tree)
61
+	templateUsage.Walk(file.Ast)

+ 7
- 7
lib/ast/walker/type_checker.go View File

@@ -1,7 +1,7 @@
1 1
 package walker
2 2
 
3 3
 import (
4
-	"github.com/champii/og/lib/ast"
4
+	"github.com/champii/og/lib/common"
5 5
 )
6 6
 
7 7
 type FuncSig struct {
@@ -72,22 +72,22 @@ type TypeChecker struct {
72 72
 	stack *Stack
73 73
 }
74 74
 
75
-func (this *TypeChecker) VarSpec(n ast.INode) ast.INode {
75
+func (this *TypeChecker) VarSpec(n common.INode) common.INode {
76 76
 	return n
77 77
 }
78
-func (this *TypeChecker) Assignment(n ast.INode) ast.INode {
78
+func (this *TypeChecker) Assignment(n common.INode) common.INode {
79 79
 	return n
80 80
 }
81
-func (this *TypeChecker) BeforeBlock(n ast.INode) {
81
+func (this *TypeChecker) BeforeBlock(n common.INode) {
82 82
 	this.stack.PushScope()
83 83
 }
84
-func (this *TypeChecker) AfterBlock(n ast.INode) {
84
+func (this *TypeChecker) AfterBlock(n common.INode) {
85 85
 	this.stack.PopScope()
86 86
 }
87
-func (this *TypeChecker) Each(n ast.INode) ast.INode {
87
+func (this *TypeChecker) Each(n common.INode) common.INode {
88 88
 	return n
89 89
 }
90
-func TypeCheck(ast ast.INode) {
90
+func TypeCheck(ast common.INode) {
91 91
 	t := TypeChecker{stack: &Stack{}}
92 92
 	t.stack.PushScope()
93 93
 	t.type_ = &t

+ 7
- 7
lib/ast/walker/type_checker.og View File

@@ -1,7 +1,7 @@
1 1
 !walker
2 2
 
3 3
 import
4
-	"github.com/champii/og/lib/ast"
4
+	"github.com/champii/og/lib/common"
5 5
 
6 6
 struct FuncSig
7 7
 	name      string
@@ -57,28 +57,28 @@ struct TypeChecker
57 57
 	AstWalker
58 58
 	stack *Stack
59 59
 
60
-	*VarSpec(n ast.INode): ast.INode ->
60
+	*VarSpec(n common.INode): common.INode ->
61 61
 		// fmt.Printf("VarSpec %#v\n", n)
62 62
 		n
63 63
 
64
-	*Assignment(n ast.INode): ast.INode ->
64
+	*Assignment(n common.INode): common.INode ->
65 65
 		// fmt.Printf("ASSIGN %#v\n", n)
66 66
 		n
67 67
 
68
-	*BeforeBlock(n ast.INode) ->
68
+	*BeforeBlock(n common.INode) ->
69 69
 		// fmt.Println("PUSH")
70 70
 		@stack.PushScope()
71 71
 
72
-	*AfterBlock(n ast.INode) ->
72
+	*AfterBlock(n common.INode) ->
73 73
 		// fmt.Println("POP")
74 74
 		@stack.PopScope()
75 75
 
76
-	*Each(n ast.INode): ast.INode -> n
76
+	*Each(n common.INode): common.INode -> n
77 77
 	// name := reflect.TypeOf(n).String()[5:]
78 78
 
79 79
 	// fmt.Printf("%s\n", strings.Repeat(" ", @indent) + name)
80 80
 
81
-TypeCheck(ast ast.INode) ->
81
+TypeCheck(ast common.INode) ->
82 82
 	t := TypeChecker
83 83
 		stack: &Stack{}
84 84
 

+ 6
- 6
lib/ast/walker/walker.go View File

@@ -1,7 +1,7 @@
1 1
 package walker
2 2
 
3 3
 import (
4
-	"github.com/champii/og/lib/ast"
4
+	"github.com/champii/og/lib/common"
5 5
 	"reflect"
6 6
 )
7 7
 
@@ -21,8 +21,8 @@ func (this *AstWalker) callDelegate(name string, arg reflect.Value) ([]reflect.V
21 21
 	}
22 22
 	return []reflect.Value{reflect.Zero(arg.Type())}, false
23 23
 }
24
-func (this *AstWalker) Trigger(arg reflect.Value, parentField reflect.Value, parentNode ast.INode) (reflect.Value, bool) {
25
-	node := arg.Interface().(ast.INode)
24
+func (this *AstWalker) Trigger(arg reflect.Value, parentField reflect.Value, parentNode common.INode) (reflect.Value, bool) {
25
+	node := arg.Interface().(common.INode)
26 26
 	if node == nil {
27 27
 		return reflect.Zero(arg.Type()), false
28 28
 	}
@@ -32,20 +32,20 @@ func (this *AstWalker) Trigger(arg reflect.Value, parentField reflect.Value, par
32 32
 	this.callDelegate("Before"+name, arg)
33 33
 	res, ok := this.callDelegate("Each", arg)
34 34
 	if ok {
35
-		node = res[0].Interface().(ast.INode)
35
+		node = res[0].Interface().(common.INode)
36 36
 		parentField.Set(reflect.ValueOf(node))
37 37
 	}
38 38
 	this.callDelegate(name, arg)
39 39
 	parentField.Set(reflect.ValueOf(this.Walk(node)))
40 40
 	res, ok = this.callDelegate("After", arg)
41 41
 	if ok {
42
-		node = res[0].Interface().(ast.INode)
42
+		node = res[0].Interface().(common.INode)
43 43
 		parentField.Set(reflect.ValueOf(node))
44 44
 	}
45 45
 	this.callDelegate("After"+name, arg)
46 46
 	return reflect.ValueOf(node), true
47 47
 }
48
-func (this *AstWalker) Walk(tree ast.INode) ast.INode {
48
+func (this *AstWalker) Walk(tree common.INode) common.INode {
49 49
 	val := reflect.ValueOf(tree).Elem()
50 50
 	for i := 0; i < val.NumField(); i++ {
51 51
 		valueField := val.Field(i)

+ 6
- 6
lib/ast/walker/walker.og View File

@@ -3,7 +3,7 @@
3 3
 import
4 4
 	// fmt
5 5
 	reflect
6
-	"github.com/champii/og/lib/ast"
6
+	"github.com/champii/og/lib/common"
7 7
 
8 8
 struct AstWalker
9 9
 	type_ interface
@@ -20,8 +20,8 @@ struct AstWalker
20 20
 
21 21
 		return []reflect.Value{reflect.Zero(arg.Type())}, false
22 22
 
23
-	*Trigger(arg reflect.Value, parentField reflect.Value, parentNode ast.INode): reflect.Value, bool->
24
-		node := arg.Interface().(ast.INode)
23
+	*Trigger(arg reflect.Value, parentField reflect.Value, parentNode common.INode): reflect.Value, bool->
24
+		node := arg.Interface().(common.INode)
25 25
 
26 26
 		if node == nil
27 27
 			return reflect.Zero(arg.Type()), false
@@ -35,7 +35,7 @@ struct AstWalker
35 35
 
36 36
 		res, ok := @callDelegate("Each", arg)
37 37
 		if ok
38
-			node = res[0].Interface().(ast.INode)
38
+			node = res[0].Interface().(common.INode)
39 39
 			parentField.Set(reflect.ValueOf(node))
40 40
 
41 41
 		@callDelegate(name, arg)
@@ -43,14 +43,14 @@ struct AstWalker
43 43
 		parentField.Set(reflect.ValueOf(@Walk(node)))
44 44
 		res, ok = @callDelegate("After", arg)
45 45
 		if ok
46
-			node = res[0].Interface().(ast.INode)
46
+			node = res[0].Interface().(common.INode)
47 47
 			parentField.Set(reflect.ValueOf(node))
48 48
 
49 49
 		@callDelegate("After" + name, arg)
50 50
 
51 51
 		return reflect.ValueOf(node), true
52 52
 
53
-	*Walk(tree ast.INode): ast.INode ->
53
+	*Walk(tree common.INode): common.INode ->
54 54
 		val := reflect.ValueOf(tree).Elem()
55 55
 
56 56
 		for i := 0; i < val.NumField(); i++

lib/og/config.go → lib/common/config.go View File

@@ -1,4 +1,4 @@
1
-package og
1
+package common
2 2
 
3 3
 type OgConfig struct {
4 4
 	Blocks      bool
@@ -6,6 +6,7 @@ type OgConfig struct {
6 6
 	Print       bool
7 7
 	Force       bool
8 8
 	Ast         bool
9
+	SimpleAst   bool
9 10
 	Quiet       bool
10 11
 	Interpreter bool
11 12
 	Paths       []string

lib/og/config.og → lib/common/config.og View File

@@ -1,4 +1,4 @@
1
-!og
1
+!common
2 2
 
3 3
 struct OgConfig
4 4
   Blocks      bool
@@ -6,6 +6,7 @@ struct OgConfig
6 6
   Print       bool
7 7
   Force       bool
8 8
   Ast         bool
9
+  SimpleAst   bool
9 10
   Quiet       bool
10 11
   Interpreter bool
11 12
   Paths       []string // from command line

+ 21
- 0
lib/common/error.go View File

@@ -0,0 +1,21 @@
1
+package common
2
+
3
+type Error struct {
4
+	Path   string
5
+	Source []string
6
+	Line   int
7
+	Column int
8
+	Msg    string
9
+	Msg2   string
10
+}
11
+
12
+func NewError(filePath string, source []string, line, column int, msg, msg2 string) *Error {
13
+	return &Error{
14
+		Path:   filePath,
15
+		Source: source,
16
+		Line:   line,
17
+		Column: column,
18
+		Msg:    msg,
19
+		Msg2:   msg2,
20
+	}
21
+}

+ 18
- 0
lib/common/error.og View File

@@ -0,0 +1,18 @@
1
+!common
2
+
3
+struct Error
4
+	Path   string
5
+	Source []string
6
+	Line   int
7
+	Column int
8
+	Msg    string
9
+	Msg2   string
10
+
11
+NewError(filePath string, source []string, line, column int, msg, msg2 string): *Error ->
12
+	&Error
13
+		Path:   filePath
14
+		Source: source
15
+		Line:   line
16
+		Column: column
17
+		Msg:    msg
18
+		Msg2:   msg2

+ 12
- 7
lib/common/file.go View File

@@ -3,21 +3,22 @@ package common
3 3
 import (
4 4
 	"errors"
5 5
 	"fmt"
6
-	"github.com/champii/og/lib/ast"
7 6
 	"io/ioutil"
8 7
 	"os"
9 8
 	"os/exec"
10 9
 	"path"
11 10
 	"path/filepath"
11
+	"strings"
12 12
 )
13 13
 
14 14
 type File struct {
15
-	Path    string
16
-	OutPath string
17
-	Name    string
18
-	Ast     ast.INode
19
-	Output  string
20
-	Source  []byte
15
+	Path        string
16
+	OutPath     string
17
+	Name        string
18
+	Ast         INode
19
+	Output      string
20
+	LineMapping []int
21
+	Source      []byte
21 22
 }
22 23
 
23 24
 func (this File) Write() {
@@ -41,6 +42,10 @@ func (this *File) Format() error {
41 42
 	this.Output = string(final)
42 43
 	return nil
43 44
 }
45
+func (this *File) Error(line, column int, msg, msg2 string) *Error {
46
+	source := strings.Split(string(this.Source), "\n")
47
+	return NewError(this.Path, source, line, column, msg, msg2)
48
+}
44 49
 func NewFile(filePath, outPath string) *File {
45 50
 	name := path.Base(filePath)
46 51
 	source, err := ioutil.ReadFile(filePath)

+ 6
- 3
lib/common/file.og View File

@@ -5,17 +5,18 @@ import
5 5
   fmt
6 6
   path
7 7
   errors
8
+  strings
8 9
   "os/exec"
9 10
   "io/ioutil"
10 11
   "path/filepath"
11
-  "github.com/champii/og/lib/ast"
12 12
 
13 13
 struct File
14 14
   Path    string
15 15
   OutPath string
16 16
   Name    string
17
-  Ast     ast.INode
17
+  Ast     INode
18 18
   Output  string
19
+  LineMapping []int
19 20
   Source  []byte
20 21
   Write ->
21 22
     os.MkdirAll(filepath.Dir(@OutPath), os.ModePerm);
@@ -42,7 +43,9 @@ struct File
42 43
     @Output = string(final)
43 44
 
44 45
     return nil
45
-
46
+  Error(line, column int, msg, msg2 string): *Error ->
47
+    source := strings.Split(string(@Source), "\n")
48
+    NewError(@Path, source, line, column, msg, msg2)
46 49
 
47 50
 NewFile(filePath, outPath string): *File ->
48 51
   name := path.Base(filePath)

+ 67
- 0
lib/common/node.go View File

@@ -0,0 +1,67 @@
1
+package common
2
+
3
+import (
4
+	"github.com/antlr/antlr4/runtime/Go/antlr"
5
+)
6
+
7
+type INode interface {
8
+	Eval() string
9
+	Text() string
10
+	Line() int
11
+	Col() int
12
+	SetParent(n INode)
13
+	GetParent() INode
14
+	ChildrenCount() int
15
+	T() interface{}
16
+}
17
+type Node struct {
18
+	Line_          int
19
+	Col_           int
20
+	Text_          string
21
+	Children       []INode
22
+	parent         INode
23
+	ChildrenCount_ int
24
+	t              interface{}
25
+}
26
+
27
+func (this Node) Eval() string {
28
+	return ""
29
+}
30
+func (this Node) Text() string {
31
+	return this.Text_
32
+}
33
+func (this Node) Line() int {
34
+	return this.Line_
35
+}
36
+func (this Node) Col() int {
37
+	return this.Col_
38
+}
39
+func (this Node) ChildrenCount() int {
40
+	return this.ChildrenCount_
41
+}
42
+func (this *Node) SetParent(n INode) {
43
+	if this.parent == nil {
44
+		this.parent = n
45
+	}
46
+}
47
+func (this *Node) GetParent() INode {
48
+	return this.parent
49
+}
50
+func (this Node) T() interface{} {
51
+	return this.t
52
+}
53
+func NewNode(ctx antlr.ParserRuleContext, file *File, t interface{}) *Node {
54
+	tok := ctx.GetStart()
55
+	line := file.LineMapping[tok.GetLine()]
56
+	col := tok.GetColumn()
57
+	return &Node{
58
+		Text_:          ctx.GetText(),
59
+		Line_:          line,
60
+		Col_:           col,
61
+		ChildrenCount_: len(ctx.GetChildren()),
62
+		t:              t,
63
+	}
64
+}
65
+func NewNodeNoCtx(t interface{}) *Node {
66
+	return &Node{t: t}
67
+}

+ 48
- 0
lib/common/node.og View File

@@ -0,0 +1,48 @@
1
+!common
2
+
3
+import
4
+	"github.com/antlr/antlr4/runtime/Go/antlr"
5
+
6
+interface INode
7
+	Eval: string
8
+	Text: string
9
+	Line: int
10
+	Col:  int
11
+	SetParent(n INode)
12
+	GetParent: INode
13
+	ChildrenCount: int
14
+	T: interface
15
+
16
+struct Node
17
+	Line_     int
18
+	Col_      int
19
+	Text_     string
20
+	Children  []INode
21
+	parent    INode
22
+	ChildrenCount_ int
23
+	t interface
24
+
25
+	Eval: string ->  ""
26
+	Text: string -> @Text_
27
+	Line: int    -> @Line_
28
+	Col:  int    -> @Col_
29
+	ChildrenCount:  int    -> @ChildrenCount_
30
+	*SetParent(n INode) -> if @parent == nil => @parent = n;
31
+	*GetParent: INode -> @parent
32
+	T: interface -> @t
33
+
34
+NewNode(ctx antlr.ParserRuleContext, file *File, t interface): *Node ->
35
+	tok := ctx.GetStart()
36
+	line := file.LineMapping[tok.GetLine()]
37
+	col := tok.GetColumn();
38
+
39
+	&Node
40
+		Text_: ctx.GetText()
41
+		Line_: line
42
+		Col_:  col
43
+		ChildrenCount_: len(ctx.GetChildren())
44
+		t: t
45
+
46
+NewNodeNoCtx(t interface): *Node ->
47
+	&Node
48
+		t: t

lib/og/printer.go → lib/common/printer.go View File

@@ -1,11 +1,32 @@
1
-package og
1
+package common
2 2
 
3 3
 import (
4
+	"fmt"
4 5
 	tm "github.com/buger/goterm"
6
+	"github.com/fatih/color"
5 7
 	curs "github.com/k0kubun/go-ansi"
6 8
 	"strconv"
7 9
 )
8 10
 
11
+var (
12
+	yellow = color.New(color.FgHiYellow).SprintFunc()
13
+)
14
+var (
15
+	red = color.New(color.FgHiRed).SprintFunc()
16
+)
17
+var (
18
+	cyan = color.New(color.FgCyan).SprintFunc()
19
+)
20
+var (
21
+	magenta = color.New(color.Bold, color.FgHiMagenta).SprintFunc()
22
+)
23
+var (
24
+	blue = color.New(color.Bold, color.FgHiBlue).SprintfFunc()
25
+)
26
+var (
27
+	green = color.New(color.FgHiGreen).SprintfFunc()
28
+)
29
+
9 30
 type Printer struct {
10 31
 	Config  *OgConfig
11 32
 	spinner int
@@ -72,6 +93,19 @@ func (this *Printer) CompileList(files []string, workerIds []int, nbWorkers, fin
72 93
 	this.spinner += 1
73 94
 	this.spinner %= 4
74 95
 }
96
+func (this *Printer) Error(err *Error) {
97
+	for i := 0; i < 8; i++ {
98
+		tm.Println("                                                                          ")
99
+	}
100
+	tm.MoveCursorUp(9)
101
+	tm.Flush()
102
+	fileInfo := fmt.Sprintf("%s (%s:%s)", green(err.Path), yellow(err.Line), yellow(err.Column))
103
+	fmt.Printf("\n%s: %s '%s'\n", fileInfo, red(err.Msg), magenta(err.Msg2))
104
+	badLine := err.Source[err.Line-1]
105
+	badLine = cyan(badLine[:err.Column]) + magenta(err.Msg2) + cyan(badLine[err.Column+len(err.Msg2):])
106
+	fmt.Println(badLine)
107
+	fmt.Print(blue("%"+strconv.Itoa(err.Column+1)+"s\n", "^"))
108
+}
75 109
 func (this Printer) CursorHide() {
76 110
 	if !this.Config.Quiet {
77 111
 		curs.CursorHide()
@@ -85,3 +119,7 @@ func (this Printer) CursorShow() {
85 119
 func NewPrinter(config *OgConfig) *Printer {
86 120
 	return &Printer{Config: config}
87 121
 }
122
+
123
+var (
124
+	Print *Printer
125
+)

lib/og/printer.og → lib/common/printer.og View File

@@ -1,10 +1,19 @@
1
-!og
1
+!common
2 2
 
3 3
 import
4
+  fmt
4 5
   strconv
6
+  "github.com/fatih/color"
5 7
   "github.com/buger/goterm": tm
6 8
   "github.com/k0kubun/go-ansi": curs
7 9
 
10
+var yellow = color.New(color.FgHiYellow).SprintFunc()
11
+var red = color.New(color.FgHiRed).SprintFunc()
12
+var cyan = color.New(color.FgCyan).SprintFunc()
13
+var magenta = color.New(color.Bold, color.FgHiMagenta).SprintFunc()
14
+var blue = color.New(color.Bold, color.FgHiBlue).SprintfFunc()
15
+var green = color.New(color.FgHiGreen).SprintfFunc()
16
+
8 17
 struct Printer
9 18
   Config *OgConfig
10 19
   spinner int
@@ -67,6 +76,21 @@ struct Printer
67 76
     @spinner += 1
68 77
     @spinner %= 4
69 78
 
79
+  *Error(err *Error) ->
80
+    for i := 0; i < 8; i++
81
+      tm.Println("                                                                          ")
82
+    tm.MoveCursorUp(9)
83
+    tm.Flush()
84
+
85
+    fileInfo := fmt.Sprintf("%s (%s:%s)", green(err.Path), yellow(err.Line), yellow(err.Column))
86
+    fmt.Printf("\n%s: %s '%s'\n", fileInfo, red(err.Msg), magenta(err.Msg2))
87
+
88
+    badLine := err.Source[err.Line-1]
89
+    badLine = cyan(badLine[:err.Column]) + magenta(err.Msg2) + cyan(badLine[err.Column+len(err.Msg2):])
90
+
91
+    fmt.Println(badLine)
92
+    fmt.Print(blue("%"+strconv.Itoa(err.Column+1)+"s\n","^"))
93
+
70 94
   CursorHide -> if !@Config.Quiet => curs.CursorHide()
71 95
   CursorShow -> if !@Config.Quiet => curs.CursorShow()
72 96
 
@@ -74,3 +98,5 @@ struct Printer
74 98
 NewPrinter(config *OgConfig): *Printer ->
75 99
   &Printer
76 100
     Config: config
101
+
102
+var Print *Printer

+ 3
- 5
lib/og/compile.go View File

@@ -11,10 +11,9 @@ import (
11 11
 )
12 12
 
13 13
 type OgCompiler struct {
14
-	Config  *OgConfig
14
+	Config  *common.OgConfig
15 15
 	Parser  *OgParser
16 16
 	Preproc *OgPreproc
17
-	Printer *Printer
18 17
 	Files   []*common.File
19 18
 }
20 19
 
@@ -34,7 +33,7 @@ func (this *OgCompiler) Compile() error {
34 33
 	if len(this.Files) < poolSize {
35 34
 		poolSize = len(this.Files)
36 35
 	}
37
-	pool := NewPool(poolSize, len(this.Files), this.Printer, this.ParseFile)
36
+	pool := NewPool(poolSize, len(this.Files), this.ParseFile)
38 37
 	for _, file := range this.Files {
39 38
 		pool.Queue(file)
40 39
 	}
@@ -116,10 +115,9 @@ func (this OgCompiler) getNewPath(filePath string) string {
116 115
 	}
117 116
 	return strings.Replace(path.Join(this.Config.OutPath, filePath), ".og", ".go", 1)
118 117
 }
119
-func NewOgCompiler(config *OgConfig, printer *Printer) *OgCompiler {
118
+func NewOgCompiler(config *common.OgConfig) *OgCompiler {
120 119
 	return &OgCompiler{
121 120
 		Config:  config,
122
-		Printer: printer,
123 121
 		Parser:  NewOgParser(config),
124 122
 		Preproc: NewOgPreproc(),
125 123
 		Files:   []*common.File{},

+ 3
- 5
lib/og/compile.og View File

@@ -10,10 +10,9 @@ import
10 10
   "github.com/champii/og/lib/ast/walker"
11 11
 
12 12
 struct OgCompiler
13
-  Config   *OgConfig
13
+  Config   *common.OgConfig
14 14
   Parser   *OgParser
15 15
   Preproc  *OgPreproc
16
-  Printer  *Printer
17 16
   Files    []*common.File
18 17
 
19 18
   *Compile: error ->
@@ -32,7 +31,7 @@ struct OgCompiler
32 31
     if len(@Files) < poolSize
33 32
       poolSize = len(@Files)
34 33
 
35
-    pool := NewPool(poolSize, len(@Files), @Printer, @ParseFile)
34
+    pool := NewPool(poolSize, len(@Files), @ParseFile)
36 35
 
37 36
     for _, file in @Files
38 37
       pool.Queue(file)
@@ -116,10 +115,9 @@ struct OgCompiler
116 115
 
117 116
     strings.Replace(path.Join(@Config.OutPath, filePath), ".og", ".go", 1)
118 117
 
119
-NewOgCompiler(config *OgConfig, printer *Printer): *OgCompiler ->
118
+NewOgCompiler(config *common.OgConfig): *OgCompiler ->
120 119
   &OgCompiler
121 120
     Config:  config
122
-    Printer: printer
123 121
     Parser:  NewOgParser(config)
124 122
     Preproc: NewOgPreproc()
125 123
     Files:   []*common.File{}

+ 10
- 11
lib/og/og.go View File

@@ -2,15 +2,15 @@ package og
2 2
 
3 3
 import (
4 4
 	"fmt"
5
+	"github.com/champii/og/lib/common"
5 6
 	"os"
6 7
 	"os/exec"
7 8
 	"path"
8 9
 )
9 10
 
10 11
 type Og struct {
11
-	Config   *OgConfig
12
+	Config   *common.OgConfig
12 13
 	Compiler *OgCompiler
13
-	Printer  *Printer
14 14
 }
15 15
 
16 16
 func (this Og) Run() error {
@@ -25,12 +25,12 @@ func (this Og) Run() error {
25 25
 		return err
26 26
 	}
27 27
 	if len(this.Compiler.Files) == 0 {
28
-		this.Printer.NothingToDo()
28
+		common.Print.NothingToDo()
29 29
 		if !this.Config.Run {
30 30
 			return nil
31 31
 		}
32 32
 	}
33
-	if this.Config.Print || this.Config.Ast || this.Config.Blocks || this.Config.Dirty {
33
+	if this.Config.Print || this.Config.Ast || this.Config.SimpleAst || this.Config.Blocks || this.Config.Dirty {
34 34
 		return nil
35 35
 	}
36 36
 	if !this.Config.NoBuild {
@@ -46,7 +46,7 @@ func (this Og) Run() error {
46 46
 	return nil
47 47
 }
48 48
 func (this Og) Build() error {
49
-	this.Printer.Compiling(len(this.Compiler.Files))
49
+	common.Print.Compiling(len(this.Compiler.Files))
50 50
 	cmd := exec.Command("go", "build")
51 51
 	out, err := cmd.CombinedOutput()
52 52
 	if err != nil {
@@ -54,7 +54,7 @@ func (this Og) Build() error {
54 54
 		return err
55 55
 	}
56 56
 	if len(this.Compiler.Files) > 0 {
57
-		this.Printer.Compiled(len(this.Compiler.Files))
57
+		common.Print.Compiled(len(this.Compiler.Files))
58 58
 	}
59 59
 	return nil
60 60
 }
@@ -64,7 +64,7 @@ func (this Og) RunBinary() error {
64 64
 		return err
65 65
 	}
66 66
 	current := path.Base(dir)
67
-	this.Printer.Running()
67
+	common.Print.Running()
68 68
 	cmd := exec.Command("./" + current)
69 69
 	cmd.Stdout = os.Stdout
70 70
 	cmd.Stderr = os.Stderr
@@ -74,11 +74,10 @@ func (this Og) RunBinary() error {
74 74
 	cmd.Wait()
75 75
 	return nil
76 76
 }
77
-func NewOg(config *OgConfig) *Og {
78
-	printer := NewPrinter(config)
77
+func NewOg(config *common.OgConfig) *Og {
78
+	common.Print = common.NewPrinter(config)
79 79
 	return &Og{
80 80
 		Config:   config,
81
-		Compiler: NewOgCompiler(config, printer),
82
-		Printer:  printer,
81
+		Compiler: NewOgCompiler(config),
83 82
 	}
84 83
 }

+ 10
- 11
lib/og/og.og View File

@@ -5,11 +5,11 @@ import
5 5
   fmt
6 6
   path
7 7
   "os/exec"
8
+  "github.com/champii/og/lib/common"
8 9
 
9 10
 struct Og
10
-  Config   *OgConfig
11
+  Config   *common.OgConfig
11 12
   Compiler *OgCompiler
12
-  Printer  *Printer
13 13
 
14 14
   Run: error ->
15 15
     if len(@Config.Paths) == 0
@@ -23,11 +23,11 @@ struct Og
23 23
       return err
24 24
 
25 25
     if len(@Compiler.Files) == 0
26
-      @Printer.NothingToDo()
26
+      common.Print.NothingToDo()
27 27
       if !@Config.Run
28 28
         return nil
29 29
 
30
-    if @Config.Print || @Config.Ast || @Config.Blocks || @Config.Dirty
30
+    if @Config.Print || @Config.Ast || @Config.SimpleAst || @Config.Blocks || @Config.Dirty
31 31
       return nil
32 32
 
33 33
     if !@Config.NoBuild
@@ -41,7 +41,7 @@ struct Og
41 41
     return nil
42 42
 
43 43
   Build: error ->
44
-    @Printer.Compiling(len(@Compiler.Files))
44
+    common.Print.Compiling(len(@Compiler.Files))
45 45
 
46 46
     cmd := exec.Command("go", "build")
47 47
 
@@ -52,7 +52,7 @@ struct Og
52 52
       return err
53 53
 
54 54
     if len(@Compiler.Files) > 0
55
-      @Printer.Compiled(len(@Compiler.Files))
55
+      common.Print.Compiled(len(@Compiler.Files))
56 56
 
57 57
     nil
58 58
 
@@ -64,7 +64,7 @@ struct Og
64 64
 
65 65
     current := path.Base(dir)
66 66
 
67
-    @Printer.Running()
67
+    common.Print.Running()
68 68
 
69 69
     cmd := exec.Command("./" + current)
70 70
     cmd.Stdout = os.Stdout
@@ -77,10 +77,9 @@ struct Og
77 77
 
78 78
     return nil
79 79
 
80
-NewOg(config *OgConfig): *Og ->
81
-  printer := NewPrinter(config);
80
+NewOg(config *common.OgConfig): *Og ->
81
+  common.Print = common.NewPrinter(config);
82 82
 
83 83
   &Og
84 84
     Config: config
85
-    Compiler: NewOgCompiler(config, printer)
86
-    Printer: printer
85
+    Compiler: NewOgCompiler(config)

+ 7
- 39
lib/og/parse.go View File

@@ -2,39 +2,16 @@ package og
2 2
 
3 3
 import (
4 4
 	"errors"
5
-	"fmt"
6 5
 	"github.com/antlr/antlr4/runtime/Go/antlr"
7
-	tm "github.com/buger/goterm"
8 6
 	"github.com/champii/og/lib/ast"
9 7
 	"github.com/champii/og/lib/ast/walker"
10 8
 	"github.com/champii/og/lib/common"
11 9
 	"github.com/champii/og/lib/translator"
12 10
 	"github.com/champii/og/parser"
13
-	"github.com/fatih/color"
14 11
 	"os"
15
-	"strconv"
16 12
 	"strings"
17 13
 )
18 14
 
19
-var (
20
-	yellow = color.New(color.FgHiYellow).SprintFunc()
21
-)
22
-var (
23
-	red = color.New(color.FgHiRed).SprintFunc()
24
-)
25
-var (
26
-	cyan = color.New(color.FgCyan).SprintFunc()
27
-)
28
-var (
29
-	magenta = color.New(color.Bold, color.FgHiMagenta).SprintFunc()
30
-)
31
-var (
32
-	blue = color.New(color.Bold, color.FgHiBlue).SprintfFunc()
33
-)
34
-var (
35
-	green = color.New(color.FgHiGreen).SprintfFunc()
36
-)
37
-
38 15
 type ErrorHandler struct {
39 16
 	*antlr.DefaultErrorStrategy
40 17
 }
@@ -53,18 +30,8 @@ type ErrorListener struct {
53 30
 }
54 31
 
55 32
 func (this *ErrorListener) SyntaxError(rec antlr.Recognizer, offendingSymbol interface{}, line, column int, msg string, e antlr.RecognitionException) {
56
-	for i := 0; i < 8; i++ {
57
-		tm.Println("                                                                          ")
58
-	}
59