diff options
author | Suleyman Farajli <suleyman@farajli.net> | 2025-01-05 00:25:12 +0400 |
---|---|---|
committer | Suleyman Farajli <suleyman@farajli.net> | 2025-01-05 00:25:12 +0400 |
commit | 95b9de4ae592b251b492c3b28dc94830e33b807d (patch) | |
tree | 901128329d75e99d6deae9fbfb527416cd5cff15 | |
parent | 95e01963d2632f34e7b1f122960ff55799a81ed2 (diff) |
recursive includes handled
-rw-r--r-- | src/main.go | 8 |
1 files changed, 6 insertions, 2 deletions
diff --git a/src/main.go b/src/main.go index 46588a9..1f48639 100644 --- a/src/main.go +++ b/src/main.go @@ -31,8 +31,6 @@ type Macro struct { tokens[] Token } -/* FIXME: better name */ - func tokenize(path string) []Token { var tokens []Token; @@ -83,6 +81,8 @@ func tokenize(path string) []Token { } /* FIXME: handle recursive includes */ func preprocess(rawTokens[] Token) []Token { + incDepth := 0 + const incDepthLim = 200 var tokens[] Token var macros[] Macro for i := 0; i < len(rawTokens); i++ { @@ -115,10 +115,14 @@ func preprocess(rawTokens[] Token) []Token { if rawTokens[i + 1].str[0] != '"' { panic("include file must be wrapped with quotes") } + if incDepth == incDepthLim { + panic("nested depth exceeds 200") + } tmp := rawTokens[i + 1].str incFile := tmp[1:len(tmp) -1 ] /* get rid of the `"` at the beginning and at the end */ rawTokens = slices.Insert(rawTokens, i + 1, tokenize(incFile)...) + incDepth++ continue case "end": |