Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epidemian
GitHub Repository: epidemian/gravity
Path: blob/master/src/compiler/gravity_semacheck2.h
1214 views
1
//
2
// gravity_semacheck2.h
3
// gravity
4
//
5
// Created by Marco Bambini on 12/09/14.
6
// Copyright (c) 2014 CreoLabs. All rights reserved.
7
//
8
9
#ifndef __GRAVITY_SEMACHECK2__
10
#define __GRAVITY_SEMACHECK2__
11
12
#include "gravity_ast.h"
13
#include "gravity_delegate.h"
14
15
// Responsible to gather and check local identifiers
16
// Complete check for all identifiers and report not found errors
17
18
bool gravity_semacheck2(gnode_t *node, gravity_delegate_t *delegate);
19
20
/*
21
22
The following table summarizes what can be defined inside a declaration:
23
24
-------+---------------------------------------------------------+
25
| func | var | enum | class | module |
26
-------+---------------------------------------------------------+
27
func | YES | YES | NO | YES | YES |
28
-------+---------------------------------------------------------+
29
var | YES | NO | NO | YES | YES |
30
-------+---------------------------------------------------------+
31
enum | YES | NO | NO | YES | YES |
32
-------+---------------------------------------------------------+
33
class | YES | NO | NO | YES | YES |
34
-------+---------------------------------------------------------+
35
module | NO | NO | NO | NO | NO |
36
-------+---------------------------------------------------------+
37
38
Everything declared inside a func is a local, so for example:
39
40
func foo {
41
func a...;
42
enum b...;
43
class c..;
44
}
45
46
is converted by codegen to:
47
48
func foo {
49
var a = func...;
50
var b = enum...;
51
var c = class..;
52
}
53
54
Even if the ONLY valid syntax is anonymous func assignment, user will not be able
55
to assign an anonymous enum or class to a variable. Restriction is applied by parser
56
and reported as a syntax error.
57
Define a module inside a function is not allowed (no real technical reason but I found
58
it a very bad programming practice), restriction is applied by samantic checker.
59
60
*/
61
62
63
// TECH NOTE:
64
// At the end of semacheck2:
65
//
66
// Each declaration and compound statement will have its own symbol table (symtable field)
67
// symtable in:
68
// NODE_LIST_STAT and NODE_COMPOUND_STAT
69
// FUNCTION_DECL and FUNCTION_EXPR
70
// ENUM_DECL
71
// CLASS_DECL
72
// MODULE_DECL
73
//
74
// Each identifier will have a reference to its declaration (symbol field)
75
// symbol field in:
76
// NODE_FILE
77
// NODE_IDENTIFIER
78
// NODE_ID
79
//
80
// Each declaration will have a reference to its enclosing declaration (env field)
81
// env field in:
82
// FUNCTION_DECL and FUNCTION_EXPR
83
// VARIABLE
84
// ENUM_DECL
85
// CLASS_DECL
86
// MODULE_DECL
87
88
#endif
89
90