├── .gitignore
├── .travis.yml
├── Cmc.sln
├── Cmc
├── Cm_AST_Design.yml
├── Cmc.csproj
├── Cmc.nuspec
├── Core
│ ├── Ast.cs
│ ├── CommandLine.cs
│ ├── Core.cs
│ ├── Environment.cs
│ ├── MetaData.cs
│ ├── Pragma.cs
│ └── ReservedWords.cs
├── Decl
│ ├── Declaration.cs
│ ├── LabelDeclaration.cs
│ └── VariableDeclaration.cs
├── Error.cs
├── Expr
│ ├── AssignmentExpression.cs
│ ├── ConstantPool.cs
│ ├── Expression.cs
│ ├── FunctionCallExpression.cs
│ ├── IfExpression.cs
│ ├── LambdaExpression.cs
│ ├── LiteralExpression.cs
│ ├── VariableExpression.cs
│ └── WhileExpression.cs
├── Modifier.cs
├── Stmt
│ ├── ReturnStatement.cs
│ ├── Statement.cs
│ └── StatementList.cs
└── Type.cs
├── CmcExec
├── CmcExec.csproj
└── Executable.cs
├── CmcTest
├── CmcTest.csproj
├── DumpCodeTest.cs
├── FunctionCallTests.cs
├── MutualRecTests.cs
├── ReturnTests.cs
├── StatementTests.cs
├── StructTests.cs
└── TypeTests.cs
├── LICENSE
├── LLVM
├── Gen.cs
├── GenAst.cs
├── GenDeclaration.cs
├── GenExpression.cs
├── GenStatement.cs
└── LLVM.csproj
├── LLVMTest
├── LLVMTest.csproj
├── LlvmCommandLineTests.cs
└── LlvmGenTests.cs
├── PROGRESS.md
├── Parser
├── ObjectRegex
│ └── Node.cs
├── Parser.csproj
└── ReadMe.md
├── ParserTest
├── BootStrap.cs
├── Bootstrap
│ ├── BootstrapParser.cs
│ └── Token.cs
└── ParserTest.csproj
└── README.md
/.gitignore:
--------------------------------------------------------------------------------
1 | # Created by .ignore support plugin (hsz.mobi)
2 | ### D template
3 | # Compiled Object files
4 | *.o
5 | *.obj
6 |
7 | # Compiled Dynamic libraries
8 | *.so
9 | *.dylib
10 | *.dll
11 |
12 | # LLVM
13 | *.ll
14 | *.s
15 |
16 | # Compiled Static libraries
17 | *.a
18 | *.lib
19 |
20 | # Executables
21 | *.exe
22 |
23 | # DUB
24 | .dub
25 | docs.json
26 | __dummy.html
27 | docs/
28 |
29 | # Code coverage
30 | *.lst
31 | ### C template
32 | # Prerequisites
33 | *.d
34 |
35 | # Object files
36 | *.ko
37 | *.elf
38 |
39 | # Linker output
40 | *.ilk
41 | *.map
42 | *.exp
43 |
44 | # Precompiled Headers
45 | *.gch
46 | *.pch
47 |
48 | # Libraries
49 | *.la
50 | *.lo
51 |
52 | # Shared objects (inc. Windows DLLs)
53 | *.so.*
54 | # Executables
55 | *.out
56 | *.app
57 | *.i*86
58 | *.x86_64
59 | *.hex
60 |
61 | # Debug files
62 | *.dSYM/
63 | *.su
64 | *.idb
65 | *.pdb
66 |
67 | # Kernel Module Compile Results
68 | *.mod*
69 | *.cmd
70 | modules.order
71 | Module.symvers
72 | Mkfile.old
73 | dkms.conf
74 | ### VisualStudioCode template
75 | .vscode/*
76 | !.vscode/settings.json
77 | !.vscode/tasks.json
78 | !.vscode/launch.json
79 | !.vscode/extensions.json
80 | ### VisualStudio template
81 | ## Ignore Visual Studio temporary files, build results, and
82 | ## files generated by popular Visual Studio add-ons.
83 | ##
84 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
85 |
86 | # User-specific files
87 | *.suo
88 | *.user
89 | *.userosscache
90 | *.sln.docstates
91 |
92 | # User-specific files (MonoDevelop/Xamarin Studio)
93 | *.userprefs
94 |
95 | # Build results
96 | [Dd]ebug/
97 | [Dd]ebugPublic/
98 | [Rr]elease/
99 | [Rr]eleases/
100 | x64/
101 | x86/
102 | bld/
103 | [Bb]in/
104 | [Oo]bj/
105 | [Ll]og/
106 |
107 | # Visual Studio 2015 cache/options directory
108 | .vs/
109 | # Uncomment if you have tasks that create the project's static files in wwwroot
110 | #wwwroot/
111 |
112 | # MSTest test Results
113 | [Tt]est[Rr]esult*/
114 | [Bb]uild[Ll]og.*
115 |
116 | # NUNIT
117 | *.VisualState.xml
118 | TestResult.xml
119 |
120 | # Build Results of an ATL Project
121 | [Dd]ebugPS/
122 | [Rr]eleasePS/
123 | dlldata.c
124 |
125 | # .NET Core
126 | project.lock.json
127 | project.fragment.lock.json
128 | artifacts/
129 | **/Properties/launchSettings.json
130 |
131 | *_i.c
132 | *_p.c
133 | *_i.h
134 | *.meta
135 | *.pgc
136 | *.pgd
137 | *.rsp
138 | *.sbr
139 | *.tlb
140 | *.tli
141 | *.tlh
142 | *.tmp
143 | *.tmp_proj
144 | *.log
145 | *.vspscc
146 | *.vssscc
147 | .builds
148 | *.pidb
149 | *.svclog
150 | *.scc
151 |
152 | # Chutzpah Test files
153 | _Chutzpah*
154 |
155 | # Visual C++ cache files
156 | ipch/
157 | *.aps
158 | *.ncb
159 | *.opendb
160 | *.opensdf
161 | *.sdf
162 | *.cachefile
163 | *.VC.db
164 | *.VC.VC.opendb
165 |
166 | # Visual Studio profiler
167 | *.psess
168 | *.vsp
169 | *.vspx
170 | *.sap
171 |
172 | # TFS 2012 Local Workspace
173 | $tf/
174 |
175 | # Guidance Automation Toolkit
176 | *.gpState
177 |
178 | # ReSharper is a .NET coding add-in
179 | _ReSharper*/
180 | *.[Rr]e[Ss]harper
181 | *.DotSettings.user
182 |
183 | # JustCode is a .NET coding add-in
184 | .JustCode
185 |
186 | # TeamCity is a build add-in
187 | _TeamCity*
188 |
189 | # DotCover is a Code Coverage Tool
190 | *.dotCover
191 |
192 | # Visual Studio code coverage results
193 | *.coverage
194 | *.coveragexml
195 |
196 | # NCrunch
197 | _NCrunch_*
198 | .*crunch*.local.xml
199 | nCrunchTemp_*
200 |
201 | # MightyMoose
202 | *.mm.*
203 | AutoTest.Net/
204 |
205 | # Web workbench (sass)
206 | .sass-cache/
207 |
208 | # Installshield output folder
209 | [Ee]xpress/
210 |
211 | # DocProject is a documentation generator add-in
212 | DocProject/buildhelp/
213 | DocProject/Help/*.HxT
214 | DocProject/Help/*.HxC
215 | DocProject/Help/*.hhc
216 | DocProject/Help/*.hhk
217 | DocProject/Help/*.hhp
218 | DocProject/Help/Html2
219 | DocProject/Help/html
220 |
221 | # Click-Once directory
222 | publish/
223 |
224 | # Publish Web Output
225 | *.[Pp]ublish.xml
226 | *.azurePubxml
227 |
228 | # but database connection strings (with potential passwords) will be unencrypted
229 | *.pubxml
230 | *.publishproj
231 |
232 | # Microsoft Azure Web App publish settings. Comment the next line if you want to
233 | # checkin your Azure Web App publish settings, but sensitive information contained
234 | # in these scripts will be unencrypted
235 | PublishScripts/
236 |
237 | # NuGet Packages
238 | *.nupkg
239 | # The packages folder can be ignored because of Package Restore
240 | **/packages/*
241 | # except build/, which is used as an MSBuild target.
242 | !**/packages/build/
243 | # Uncomment if necessary however generally it will be regenerated when needed
244 | #!**/packages/repositories.config
245 | # NuGet v3's project.json files produces more ignorable files
246 | *.nuget.props
247 | *.nuget.targets
248 |
249 | # Microsoft Azure Build Output
250 | csx/
251 | *.build.csdef
252 |
253 | # Microsoft Azure Emulator
254 | ecf/
255 | rcf/
256 |
257 | # Windows Store app package directories and files
258 | AppPackages/
259 | BundleArtifacts/
260 | Package.StoreAssociation.xml
261 | _pkginfo.txt
262 |
263 | # Visual Studio cache files
264 | # files ending in .cache can be ignored
265 | # but keep track of directories ending in .cache
266 | !*.[Cc]ache/
267 |
268 | # Others
269 | ClientBin/
270 | ~$*
271 | *~
272 | *.dbmdl
273 | *.dbproj.schemaview
274 | *.jfm
275 | *.pfx
276 | *.publishsettings
277 | orleans.codegen.cs
278 |
279 | # Since there are multiple workflows, uncomment next line to ignore bower_components
280 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
281 | #bower_components/
282 |
283 | # RIA/Silverlight projects
284 | Generated_Code/
285 |
286 | # Backup & report files from converting an old project file
287 | # to a newer Visual Studio version. Backup files are not needed,
288 | # because we have git ;-)
289 | _UpgradeReport_Files/
290 | Backup*/
291 | UpgradeLog*.XML
292 | UpgradeLog*.htm
293 |
294 | # SQL Server files
295 | *.mdf
296 | *.ldf
297 |
298 | # Business Intelligence projects
299 | *.rdl.data
300 | *.bim.layout
301 | *.bim_*.settings
302 |
303 | # Microsoft Fakes
304 | FakesAssemblies/
305 |
306 | # GhostDoc plugin setting file
307 | *.GhostDoc.xml
308 |
309 | # Node.js Tools for Visual Studio
310 | .ntvs_analysis.dat
311 | node_modules/
312 |
313 | # Typescript v1 declaration files
314 | typings/
315 |
316 | # Visual Studio 6 build log
317 | *.plg
318 |
319 | # Visual Studio 6 workspace options file
320 | *.opt
321 |
322 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
323 | *.vbw
324 |
325 | # Visual Studio LightSwitch build output
326 | **/*.HTMLClient/GeneratedArtifacts
327 | **/*.DesktopClient/GeneratedArtifacts
328 | **/*.DesktopClient/ModelManifest.xml
329 | **/*.Server/GeneratedArtifacts
330 | **/*.Server/ModelManifest.xml
331 | _Pvt_Extensions
332 |
333 | # Paket dependency manager
334 | .paket/paket.exe
335 | paket-files/
336 |
337 | # FAKE - F# Make
338 | .fake/
339 |
340 | # JetBrains Rider
341 | .idea/
342 | *.sln.iml
343 |
344 | # CodeRush
345 | .cr/
346 |
347 | # Python Tools for Visual Studio (PTVS)
348 | __pycache__/
349 | *.pyc
350 |
351 | # Cake - Uncomment if you are using it
352 | # tools/**
353 | # !tools/packages.config
354 | ### C++ template
355 | # Prerequisites
356 | # Compiled Object files
357 | *.slo
358 | # Precompiled Headers
359 | # Compiled Dynamic libraries
360 | # Fortran module files
361 | *.mod
362 | *.smod
363 |
364 | # Compiled Static libraries
365 | *.lai
366 | # Executables
367 |
368 | testrunner
369 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: csharp
2 | mono: none
3 | dotnet: 2.0.0
4 | dist: trusty
5 | solution: Cmc.sln
6 |
7 | #install:
8 | #- nuget restore Cmc.sln
9 | #- nuget install NUnit.Runners -Version 3.2.0 -OutputDirectory testrunner
10 |
11 | cache:
12 | apt: true
13 |
14 | script:
15 | - dotnet restore
16 | - dotnet test ./CmcTest/CmcTest.csproj
17 | - dotnet test ./LLVMTest/LLVMTest.csproj
18 | - dotnet test ./ParserTest/ParserTest.csproj
19 | #- xbuild /p:Configuration=Release Cmc.sln
20 | #- mono ./testrunner/NUnit.ConsoleRunner.3.2.0/tools/nunit3-console.exe ./Cmc-Test/bin/Release/Cmc_Test.dll
21 | # local:
22 | # mono ./testrunner/NUnit.ConsoleRunner.3.7.0/tools/nunit3-console.exe ./Cmc-Test/bin/Debug/Cmc_Test.dll
23 |
24 |
--------------------------------------------------------------------------------
/Cmc.sln:
--------------------------------------------------------------------------------
1 | Microsoft Visual Studio Solution File, Format Version 12.00
2 | # Visual Studio 2015
3 | VisualStudioVersion = 15.1.371.0
4 | MinimumVisualStudioVersion = 15.1.371.0
5 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Cmc", "Cmc\Cmc.csproj", "{730DD0D7-5966-480E-8716-34E95C7E2684}"
6 | EndProject
7 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CmcTest", "CmcTest\CmcTest.csproj", "{81B2DCB5-26FE-40BF-B942-DB192E2B7F9C}"
8 | EndProject
9 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LLVM", "LLVM\LLVM.csproj", "{4FA21EBF-A178-4E12-A610-DF9FBCC161A1}"
10 | EndProject
11 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LLVMTest", "LLVMTest\LLVMTest.csproj", "{A1B637D0-CA61-4671-A6AC-ACB7CF937F5F}"
12 | EndProject
13 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CmcExec", "CmcExec\CmcExec.csproj", "{FDDDB91D-1AAB-477A-B3E3-0B374FB85808}"
14 | EndProject
15 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Parser", "Parser\Parser.csproj", "{E95D4571-3CE3-48EF-8107-AEEB654E3A77}"
16 | EndProject
17 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ParserTest", "ParserTest\ParserTest.csproj", "{C2AFB02B-420B-4915-B094-0FC4D8CB2648}"
18 | EndProject
19 | Global
20 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
21 | Debug|Any CPU = Debug|Any CPU
22 | Release|Any CPU = Release|Any CPU
23 | EndGlobalSection
24 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
25 | {22B2B630-2AB0-46A0-B5B7-279D6FE88069}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
26 | {22B2B630-2AB0-46A0-B5B7-279D6FE88069}.Debug|Any CPU.Build.0 = Debug|Any CPU
27 | {22B2B630-2AB0-46A0-B5B7-279D6FE88069}.Release|Any CPU.ActiveCfg = Release|Any CPU
28 | {22B2B630-2AB0-46A0-B5B7-279D6FE88069}.Release|Any CPU.Build.0 = Release|Any CPU
29 | {F0F0AC0C-7DDB-4555-AD24-4724B14DBAB5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
30 | {F0F0AC0C-7DDB-4555-AD24-4724B14DBAB5}.Debug|Any CPU.Build.0 = Debug|Any CPU
31 | {F0F0AC0C-7DDB-4555-AD24-4724B14DBAB5}.Release|Any CPU.ActiveCfg = Release|Any CPU
32 | {F0F0AC0C-7DDB-4555-AD24-4724B14DBAB5}.Release|Any CPU.Build.0 = Release|Any CPU
33 | {978F4B9A-3E9A-4D7D-A7A3-10AC3A499E8E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
34 | {978F4B9A-3E9A-4D7D-A7A3-10AC3A499E8E}.Debug|Any CPU.Build.0 = Debug|Any CPU
35 | {978F4B9A-3E9A-4D7D-A7A3-10AC3A499E8E}.Release|Any CPU.ActiveCfg = Release|Any CPU
36 | {978F4B9A-3E9A-4D7D-A7A3-10AC3A499E8E}.Release|Any CPU.Build.0 = Release|Any CPU
37 | {7172A461-5A1E-4297-B3F8-F4FC4192113D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
38 | {7172A461-5A1E-4297-B3F8-F4FC4192113D}.Debug|Any CPU.Build.0 = Debug|Any CPU
39 | {7172A461-5A1E-4297-B3F8-F4FC4192113D}.Release|Any CPU.ActiveCfg = Release|Any CPU
40 | {7172A461-5A1E-4297-B3F8-F4FC4192113D}.Release|Any CPU.Build.0 = Release|Any CPU
41 | {730DD0D7-5966-480E-8716-34E95C7E2684}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
42 | {730DD0D7-5966-480E-8716-34E95C7E2684}.Debug|Any CPU.Build.0 = Debug|Any CPU
43 | {730DD0D7-5966-480E-8716-34E95C7E2684}.Release|Any CPU.ActiveCfg = Release|Any CPU
44 | {730DD0D7-5966-480E-8716-34E95C7E2684}.Release|Any CPU.Build.0 = Release|Any CPU
45 | {81B2DCB5-26FE-40BF-B942-DB192E2B7F9C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
46 | {81B2DCB5-26FE-40BF-B942-DB192E2B7F9C}.Debug|Any CPU.Build.0 = Debug|Any CPU
47 | {81B2DCB5-26FE-40BF-B942-DB192E2B7F9C}.Release|Any CPU.ActiveCfg = Release|Any CPU
48 | {81B2DCB5-26FE-40BF-B942-DB192E2B7F9C}.Release|Any CPU.Build.0 = Release|Any CPU
49 | {4FA21EBF-A178-4E12-A610-DF9FBCC161A1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
50 | {4FA21EBF-A178-4E12-A610-DF9FBCC161A1}.Debug|Any CPU.Build.0 = Debug|Any CPU
51 | {4FA21EBF-A178-4E12-A610-DF9FBCC161A1}.Release|Any CPU.ActiveCfg = Release|Any CPU
52 | {4FA21EBF-A178-4E12-A610-DF9FBCC161A1}.Release|Any CPU.Build.0 = Release|Any CPU
53 | {A1B637D0-CA61-4671-A6AC-ACB7CF937F5F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
54 | {A1B637D0-CA61-4671-A6AC-ACB7CF937F5F}.Debug|Any CPU.Build.0 = Debug|Any CPU
55 | {A1B637D0-CA61-4671-A6AC-ACB7CF937F5F}.Release|Any CPU.ActiveCfg = Release|Any CPU
56 | {A1B637D0-CA61-4671-A6AC-ACB7CF937F5F}.Release|Any CPU.Build.0 = Release|Any CPU
57 | {FDDDB91D-1AAB-477A-B3E3-0B374FB85808}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
58 | {FDDDB91D-1AAB-477A-B3E3-0B374FB85808}.Debug|Any CPU.Build.0 = Debug|Any CPU
59 | {FDDDB91D-1AAB-477A-B3E3-0B374FB85808}.Release|Any CPU.ActiveCfg = Release|Any CPU
60 | {FDDDB91D-1AAB-477A-B3E3-0B374FB85808}.Release|Any CPU.Build.0 = Release|Any CPU
61 | {E95D4571-3CE3-48EF-8107-AEEB654E3A77}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
62 | {E95D4571-3CE3-48EF-8107-AEEB654E3A77}.Debug|Any CPU.Build.0 = Debug|Any CPU
63 | {E95D4571-3CE3-48EF-8107-AEEB654E3A77}.Release|Any CPU.ActiveCfg = Release|Any CPU
64 | {E95D4571-3CE3-48EF-8107-AEEB654E3A77}.Release|Any CPU.Build.0 = Release|Any CPU
65 | {C2AFB02B-420B-4915-B094-0FC4D8CB2648}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
66 | {C2AFB02B-420B-4915-B094-0FC4D8CB2648}.Debug|Any CPU.Build.0 = Debug|Any CPU
67 | {C2AFB02B-420B-4915-B094-0FC4D8CB2648}.Release|Any CPU.ActiveCfg = Release|Any CPU
68 | {C2AFB02B-420B-4915-B094-0FC4D8CB2648}.Release|Any CPU.Build.0 = Release|Any CPU
69 | EndGlobalSection
70 | GlobalSection(SolutionProperties) = preSolution
71 | HideSolutionNode = FALSE
72 | EndGlobalSection
73 | EndGlobal
74 |
--------------------------------------------------------------------------------
/Cmc/Cm_AST_Design.yml:
--------------------------------------------------------------------------------
1 | Ast:
2 | Statement:
3 | AssignmentStatement
4 | ExpressionStatement:
5 | ReturnStatement
6 | JumpStatement
7 | StatementList
8 | ConditionalStatement:
9 | WhileStatement
10 | IfStatement
11 | Declaration:
12 | VariableDeclaration
13 | StructDeclaration
14 | TypeDeclaration
15 | ReturnLabelDeclaration
16 | JumpLabelDeclaration
17 | Expression:
18 | FunctionCallExpression
19 | MemberAccessExpression
20 | AtomicExpression:
21 | VariableExpression
22 | LiteralExpression:
23 | NullExpression
24 | IntLiteralExpression
25 | FloatLiteralExpression
26 | BoolLiteralExpression
27 | StringLiteralExpression
28 | LambdaExpression
29 | Type:
30 | PrimaryType
31 | SecondaryType
32 | LambdaType
33 |
34 |
--------------------------------------------------------------------------------
/Cmc/Cmc.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Debug
5 | AnyCPU
6 | {17E0DE41-2426-4616-8424-BA6C0F97F7C0}
7 | {FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}
8 | Library
9 | Properties
10 | Cmc
11 | Cmc
12 | netcoreapp2.0
13 | 512
14 |
15 |
16 |
17 |
18 | 2.0
19 | true
20 | true
21 | The compiler for the Cm programming language
22 | https://www.gnu.org/licenses/gpl-3.0.en.html
23 | https://github.com/Cm-lang/Cmc
24 | Cm-lang
25 | ice1000
26 | First release.
27 | Cmc
28 | 0.0.1
29 | https://avatars1.githubusercontent.com/u/31237156
30 | Compiler Analyzer TypeSystem Cm Cmlang
31 |
32 |
33 |
34 |
35 |
36 |
37 |
44 |
--------------------------------------------------------------------------------
/Cmc/Cmc.nuspec:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Package
5 | 0.0.2
6 | ice1000 thautwarm
7 | ice1000 SmallLuma thautwarm
8 | https://github.com/Cm-lang/Cmc/blob/master/LICENSE
9 | https://github.com/Cm-lang/Cmc
10 | https://avatars1.githubusercontent.com/u/31237156
11 | true
12 | The compiler for the Cm programming language
13 | Second release
14 | Copyright 2017
15 | Compiler Analyzer TypeSystem Cm Cmlang
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/Cmc/Core/Ast.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using Cmc.Expr;
4 | using Cmc.Stmt;
5 | using JetBrains.Annotations;
6 |
7 | namespace Cmc.Core
8 | {
9 | public abstract class Ast
10 | {
11 | [NotNull] public static readonly Func MapFunc = i => $" {i}";
12 | [NotNull] public static readonly Func MapFunc2 = i => $" {i}";
13 | public Environment Env;
14 | public MetaData MetaData;
15 |
16 | ///
17 | /// inline/constant folding/etc.
18 | ///
19 | [CanBeNull] public StatementList OptimizedStatementList;
20 |
21 | protected Ast(MetaData metaData) => MetaData = metaData;
22 |
23 | public virtual void SurroundWith([NotNull] Environment environment) => Env = Env ?? environment;
24 |
25 | public virtual void ConvertGoto()
26 | {
27 | }
28 |
29 | ///
30 | /// FEATURE #15
31 | ///
32 | /// compilation information
33 | [NotNull]
34 | public abstract IEnumerable Dump();
35 |
36 | [NotNull]
37 | public abstract IEnumerable DumpCode();
38 |
39 | public void PrintDumpInfo() => Console.WriteLine(string.Join("", Dump()));
40 | public void PrintCode() => Console.WriteLine(string.Join("", DumpCode()));
41 | }
42 | }
--------------------------------------------------------------------------------
/Cmc/Core/CommandLine.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Diagnostics;
3 |
4 | namespace Cmc.Core
5 | {
6 | public static class CommandLine
7 | {
8 | ///
9 | /// Will select os automatically
10 | ///
11 | public static void RunCommand(string command)
12 | {
13 | Console.WriteLine(command);
14 | var cmd = new Process
15 | {
16 | StartInfo =
17 | {
18 | RedirectStandardInput = true,
19 | RedirectStandardOutput = true,
20 | CreateNoWindow = true,
21 | UseShellExecute = false
22 | }
23 | };
24 | var platform = System.Environment.OSVersion.Platform;
25 | switch (platform)
26 | {
27 | case PlatformID.Xbox:
28 | break;
29 | case PlatformID.Win32Windows:
30 | case PlatformID.Win32NT:
31 | case PlatformID.Win32S:
32 | case PlatformID.WinCE:
33 | cmd.StartInfo.FileName = "cmd.exe";
34 | break;
35 | case PlatformID.MacOSX:
36 | case PlatformID.Unix:
37 | cmd.StartInfo.FileName = "sh";
38 | break;
39 | default:
40 | throw new ArgumentOutOfRangeException(platform.ToString(), "unknown os!");
41 | }
42 | cmd.Start();
43 |
44 | cmd.StandardInput.WriteLine(command);
45 | cmd.StandardInput.Flush();
46 | cmd.StandardInput.Close();
47 | cmd.WaitForExit();
48 | Console.WriteLine(cmd.StandardOutput.ReadToEnd());
49 | }
50 | }
51 | }
--------------------------------------------------------------------------------
/Cmc/Core/Core.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Concurrent;
2 | using System.Collections.Generic;
3 | using System.Configuration;
4 | using System.Linq;
5 | using System.Runtime.Serialization.Formatters;
6 | using Cmc.Decl;
7 | using Cmc.Expr;
8 | using JetBrains.Annotations;
9 | using static System.StringComparison;
10 |
11 | namespace Cmc.Core
12 | {
13 | ///
14 | /// The core of this compiler
15 | /// input a file (multiple file will be supported later)
16 | ///
17 | public class Core
18 | {
19 | [NotNull] private readonly IDictionary> _mutualRecList;
20 |
21 | [NotNull] private readonly IDictionary _mutualRecMark;
22 |
23 | public Core()
24 | {
25 | _mutualRecMark = new ConcurrentDictionary();
26 | _mutualRecList = new ConcurrentDictionary>();
27 | }
28 |
29 | ///
30 | /// Do all static analyze jobs
31 | ///
32 | ///
33 | /// Parsed top-level declarations
34 | ///
35 | /// the analyzed declarations (errors are given during this process)
36 | [NotNull]
37 | public IEnumerable Analyze(params Declaration[] declarations)
38 | {
39 | var planet = new Environment(Environment.SolarSystem);
40 | var isMainDefined = false;
41 | foreach (var declaration in declarations)
42 | {
43 | planet.Declarations.Add(declaration);
44 | switch (declaration)
45 | {
46 | case VariableDeclaration variableDeclaration:
47 | variableDeclaration.IsGlobal = true;
48 | // FEATURE #40
49 | if (isMainDefined &&
50 | variableDeclaration.Expression is LambdaExpression &&
51 | string.Equals(variableDeclaration.Name, "main", Ordinal))
52 | Errors.Add($"{variableDeclaration.MetaData}you shouldn't declare more than one main function!");
53 | else isMainDefined = true;
54 | break;
55 | }
56 | }
57 | CheckMutualRec(declarations);
58 | foreach (var declaration in declarations)
59 | {
60 | declaration.SurroundWith(planet);
61 | if (!Pragma.KeepAll) declaration.ConvertGoto();
62 | }
63 | return declarations;
64 | }
65 |
66 | ///
67 | /// to check if there's mutual recursion definitions
68 | /// in the structs.
69 | /// FEATURE #34
70 | ///
71 | ///
72 | public void CheckMutualRec([NotNull] IEnumerable declarations)
73 | {
74 | var structs = new List();
75 | foreach (var keyValuePair in
76 | from i in declarations
77 | where i is StructDeclaration
78 | select
79 | new KeyValuePair>(
80 | i.Name,
81 | from q in ((StructDeclaration) i)
82 | .FieldList
83 | where !(
84 | from qq in Environment.SolarSystem.Declarations
85 | select qq.Name)
86 | .Contains(q.Type.ToString())
87 | select q.Type.Name))
88 | {
89 | structs.Add(keyValuePair.Key);
90 | _mutualRecList.Add(keyValuePair);
91 | _mutualRecMark[keyValuePair.Key] = false;
92 | }
93 | foreach (var chain in
94 | from chain in structs.Select(CheckMutualRecRec)
95 | where null != chain
96 | select chain)
97 | {
98 | Errors.Add($"mutual recursion in structure definition is detected: [{chain}]");
99 | break;
100 | }
101 | }
102 |
103 | ///
104 | /// dfs
105 | ///
106 | /// current delaration
107 | /// if not null, return the dependency chain
108 | [CanBeNull]
109 | private string CheckMutualRecRec(string declaration)
110 | {
111 | if (_mutualRecMark[declaration]) return declaration;
112 | _mutualRecMark[declaration] = true;
113 | foreach (var ret in
114 | from ret in _mutualRecList[declaration].Select(CheckMutualRecRec)
115 | where null != ret
116 | select ret)
117 | return $"{declaration}]-[{ret}";
118 | _mutualRecMark[declaration] = false;
119 | return null;
120 | }
121 | }
122 | }
--------------------------------------------------------------------------------
/Cmc/Core/Environment.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using Cmc.Decl;
5 | using Cmc.Expr;
6 | using Cmc.Stmt;
7 | using JetBrains.Annotations;
8 | using static System.StringComparison;
9 |
10 | namespace Cmc.Core
11 | {
12 | public class Environment
13 | {
14 | ///
15 | /// preload functions/types
16 | ///
17 | public static Environment Galaxy;
18 |
19 | private static readonly MetaData BuiltIn = new MetaData(-1, "[built-in]");
20 |
21 | public static Environment SolarSystem;
22 |
23 | [NotNull] public readonly IList Declarations = new List();
24 |
25 | // FEATURE #18
26 | [CanBeNull] private readonly Environment _outer;
27 |
28 | public static void Initialize()
29 | {
30 | Galaxy = new Environment();
31 | SolarSystem = new Environment(Galaxy);
32 |
33 | // FEATURE #0
34 | foreach (var typeDeclaration in
35 | from builtinType in new[]
36 | {
37 | "i8", "i16", "i32", "i64",
38 | "u8", "u16", "u32", "u64",
39 | "f32", "f64",
40 | PrimaryType.StringType,
41 | PrimaryType.NullType,
42 | PrimaryType.BoolType
43 | }
44 | select new TypeDeclaration(
45 | BuiltIn,
46 | builtinType,
47 | new PrimaryType(BuiltIn, builtinType)))
48 | Galaxy.Declarations.Add(typeDeclaration);
49 | var puts = new ExternDeclaration(BuiltIn, "print", null,
50 | new LambdaType(BuiltIn,
51 | new List
52 | {
53 | new PrimaryType(BuiltIn, PrimaryType.StringType)
54 | },
55 | new PrimaryType(BuiltIn, PrimaryType.NullType)));
56 | puts.SurroundWith(Galaxy);
57 | SolarSystem.Declarations.Add(puts);
58 | }
59 |
60 | static Environment() => Initialize();
61 |
62 | public Environment(Environment outer = null) => _outer = outer;
63 |
64 | /// FEATURE #3
65 | [NotNull]
66 | public IList FindAllDeclarationsByName([NotNull] string name)
67 | {
68 | var env = this;
69 | var list = new List();
70 | do
71 | {
72 | list.AddRange(
73 | from declaration in env.Declarations
74 | where string.Equals(declaration.Name, name, Ordinal)
75 | select declaration);
76 | } while ((env = env._outer) != null);
77 | return list;
78 | }
79 |
80 | ///
81 | /// If there's no such declaration, this funciton will return null.
82 | /// FEATURE #5
83 | ///
84 | /// the name of the required declaration
85 | /// the declaration
86 | [CanBeNull]
87 | public Declaration FindDeclarationByName([NotNull] string name) =>
88 | FindDeclarationSatisfies(i => string.Equals(i.Name, name, Ordinal));
89 |
90 | ///
91 | /// If there's no such declaration, this funciton will return null.
92 | ///
93 | ///
94 | /// the name of the required declaration.
95 | /// if it's null, find the nearest one.
96 | ///
97 | /// the declaration
98 | [CanBeNull]
99 | public ReturnLabelDeclaration FindReturnLabelByName([NotNull] string name) =>
100 | (ReturnLabelDeclaration) FindDeclarationSatisfies(i =>
101 | i is ReturnLabelDeclaration && string.Equals(i.Name, name, Ordinal));
102 |
103 | [CanBeNull]
104 | public JumpLabelDeclaration FindJumpLabelByName([NotNull] string name) =>
105 | (JumpLabelDeclaration) FindDeclarationSatisfies(i =>
106 | i is JumpLabelDeclaration && string.Equals(i.Name, name, Ordinal));
107 |
108 | ///
109 | /// Find a declaration satisfying one constraint
110 | ///
111 | /// the constraint
112 | /// the declaration
113 | [CanBeNull]
114 | public Declaration FindDeclarationSatisfies([NotNull] Predicate predicate)
115 | {
116 | var env = this;
117 | do
118 | {
119 | foreach (var declaration in
120 | from declaration in env.Declarations
121 | where predicate(declaration)
122 | select declaration)
123 | return declaration;
124 | } while ((env = env._outer) != null);
125 | return null;
126 | }
127 | }
128 | }
--------------------------------------------------------------------------------
/Cmc/Core/MetaData.cs:
--------------------------------------------------------------------------------
1 | using System.Linq;
2 | using JetBrains.Annotations;
3 |
4 | namespace Cmc.Core
5 | {
6 | public struct MetaData
7 | {
8 | public readonly int LineNumber;
9 | public readonly string FileName;
10 | public readonly string TrimedFileName;
11 |
12 | public MetaData(int lineNumber, string fileName)
13 | {
14 | LineNumber = lineNumber;
15 | FileName = fileName;
16 | TrimedFileName = string.Concat(FileName.Where(i => char.IsLetterOrDigit(i) || i == '_'));
17 | }
18 |
19 | // FEATURE #10
20 | [NotNull]
21 | public string GetErrorHeader() =>
22 | $"Error in file {FileName} at line {LineNumber}: ";
23 |
24 | private static int _count;
25 | public static readonly MetaData Empty = new MetaData(_count++, "Unknown");
26 | }
27 | }
--------------------------------------------------------------------------------
/Cmc/Core/Pragma.cs:
--------------------------------------------------------------------------------
1 | namespace Cmc.Core
2 | {
3 | public static class Pragma
4 | {
5 | public static bool Glsl = false;
6 | public static bool Hm = false;
7 | public static bool KeepAll = false;
8 | public static bool AbortAtFirst = false;
9 | public static bool PrintDumpInfo = true;
10 | }
11 | }
--------------------------------------------------------------------------------
/Cmc/Core/ReservedWords.cs:
--------------------------------------------------------------------------------
1 | using JetBrains.Annotations;
2 |
3 | namespace Cmc.Core
4 | {
5 | public static class ReservedWords
6 | {
7 | [NotNull] public const string Recur = "recur";
8 | [NotNull] public const string StringType = "string";
9 | [NotNull] public const string NullType = "nulltype";
10 | [NotNull] public const string BoolType = "bool";
11 | }
12 | }
--------------------------------------------------------------------------------
/Cmc/Decl/Declaration.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using System.Linq;
3 | using Cmc.Core;
4 | using Cmc.Stmt;
5 | using JetBrains.Annotations;
6 | using Environment = Cmc.Core.Environment;
7 |
8 | #pragma warning disable 659
9 |
10 | namespace Cmc.Decl
11 | {
12 | public class Declaration : Statement
13 | {
14 | public readonly Modifier[] Modifiers;
15 | [NotNull] public readonly string Name;
16 | [NotNull] public readonly List DupParams;
17 | public ulong UsageCount;
18 |
19 | public Declaration(
20 | MetaData metaData,
21 | [NotNull] string name,
22 | Modifier[] modifiers = null,
23 | [CanBeNull] List dupParams = null) : base(metaData)
24 | {
25 | Name = name;
26 | DupParams = dupParams ?? new List();
27 | Modifiers = modifiers;
28 | }
29 | }
30 |
31 | ///
32 | /// type aliases
33 | /// FEATURE #31
34 | ///
35 | public class TypeDeclaration : Declaration
36 | {
37 | [NotNull] public readonly Type Type;
38 |
39 | public TypeDeclaration(
40 | MetaData metaData,
41 | [NotNull] string name,
42 | [NotNull] Type type,
43 | Modifier[] modifiers = null)
44 | : base(metaData, name, modifiers ?? new[] {Modifier.Private})
45 | {
46 | Type = type;
47 | }
48 | }
49 |
50 | public class StructDeclaration : Declaration
51 | {
52 | [NotNull] public readonly IList FieldList;
53 | [NotNull] public readonly Type Type;
54 |
55 | public StructDeclaration(
56 | MetaData metaData,
57 | [NotNull] string name,
58 | [NotNull] IList fieldList,
59 | Modifier[] modifiers = null) :
60 | base(metaData, name, modifiers ?? new[] {Modifier.Private})
61 | {
62 | FieldList = fieldList;
63 | Type = new SecondaryType(metaData, name, this);
64 | }
65 |
66 | public override void SurroundWith(Environment environment)
67 | {
68 | base.SurroundWith(environment);
69 | var internalEnv = new Environment(Env);
70 | foreach (var variableDeclaration in FieldList)
71 | variableDeclaration.SurroundWith(internalEnv);
72 | }
73 |
74 | public override IEnumerable Dump() =>
75 | new[]
76 | {
77 | "struct declaration:\n",
78 | " members:\n"
79 | }
80 | .Concat(
81 | from field in FieldList
82 | from dump in field.Dump()
83 | select $" {dump}");
84 | }
85 |
86 | public class ExternDeclaration : Declaration
87 | {
88 | public readonly Type Type;
89 | public readonly bool Mutability;
90 |
91 | public ExternDeclaration(
92 | MetaData metaData,
93 | [NotNull] string name,
94 | Modifier[] modifiers,
95 | Type type,
96 | bool mutability = false) :
97 | base(metaData, name, modifiers ?? new[] {Modifier.Private})
98 | {
99 | Type = type;
100 | Mutability = mutability;
101 | }
102 |
103 | public override IEnumerable Dump() => new[]
104 | {$"extern declaration [{Name}]:\n"}
105 | .Concat(Type.Dump());
106 | }
107 |
108 | ///
109 | /// Probably useless
110 | ///
111 | public class Macro : Declaration
112 | {
113 | [NotNull] public string Content;
114 |
115 | public Macro(
116 | MetaData metaData,
117 | [NotNull] string name,
118 | [NotNull] string content,
119 | Modifier[]modifiers = null) :
120 | base(metaData, name, modifiers ?? new[] {Modifier.Private})
121 | {
122 | Content = content;
123 | }
124 |
125 | public override IEnumerable Dump() => new[]
126 | {
127 | "macro(this shouldn't appear)\n"
128 | };
129 | }
130 | }
--------------------------------------------------------------------------------
/Cmc/Decl/LabelDeclaration.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using Cmc.Core;
3 | using Cmc.Stmt;
4 | using JetBrains.Annotations;
5 |
6 | namespace Cmc.Decl
7 | {
8 | public interface ILabel
9 | {
10 | LabelDeclaration GetLabel();
11 | }
12 |
13 | ///
14 | /// return to here
15 | ///
16 | public class ReturnLabelDeclaration : Declaration, ILabel
17 | {
18 | [NotNull] public readonly IList StatementsUsingThis;
19 |
20 | public ReturnLabelDeclaration(
21 | MetaData metaData,
22 | [NotNull] string name) :
23 | base(metaData, name)
24 | {
25 | StatementsUsingThis = new List();
26 | }
27 |
28 | public sealed override string ToString() => Name.Length == 0 ? GetHashCode().ToString() : Name;
29 | public LabelDeclaration GetLabel() => new LabelDeclaration(MetaData, ToString());
30 | public override IEnumerable Dump() => new[] {$"return label [{this}]\n"};
31 | public override IEnumerable DumpCode() => new[] {$"rlabel:{this};\n"};
32 | }
33 |
34 | ///
35 | /// jump to here
36 | ///
37 | public class JumpLabelDeclaration : Declaration, ILabel
38 | {
39 | [NotNull] public readonly IList StatementsUsingThis;
40 |
41 | public JumpLabelDeclaration(
42 | MetaData metaData,
43 | [NotNull] string name) :
44 | base(metaData, name)
45 | {
46 | StatementsUsingThis = new List();
47 | }
48 |
49 | public sealed override string ToString() => Name.Length == 0 ? GetHashCode().ToString() : Name;
50 | public LabelDeclaration GetLabel() => new LabelDeclaration(MetaData, ToString());
51 | public override IEnumerable Dump() => new[] {$"jump label [{this}]\n"};
52 | public override IEnumerable DumpCode() => new[] {$"jlabel:{this};\n"};
53 | }
54 |
55 | public class LabelDeclaration : Declaration
56 | {
57 | public LabelDeclaration(
58 | MetaData metaData,
59 | [NotNull] string name) : base(metaData, name)
60 | {
61 | }
62 |
63 | public override IEnumerable Dump() => new[] {$"label [{Name}]\n"};
64 | public override IEnumerable DumpCode() => new[] {$"label:{Name};\n"};
65 | }
66 | }
--------------------------------------------------------------------------------
/Cmc/Decl/VariableDeclaration.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using System.Diagnostics;
3 | using System.Linq;
4 | using System.Runtime.InteropServices;
5 | using Cmc.Core;
6 | using Cmc.Expr;
7 | using JetBrains.Annotations;
8 | using static System.StringComparison;
9 | using Environment = Cmc.Core.Environment;
10 |
11 | #pragma warning disable 659
12 |
13 | namespace Cmc.Decl
14 | {
15 | public sealed class VariableDeclaration : Declaration
16 | {
17 | [NotNull] public Expression Expression;
18 | public bool Mutability;
19 | public bool IsGlobal = false;
20 | [CanBeNull] public Type Type;
21 |
22 | public VariableDeclaration(
23 | MetaData metaData,
24 | [NotNull] string name,
25 | [CanBeNull] Expression expression = null,
26 | bool isMutable = false,
27 | [CanBeNull] Type type = null,
28 | Modifier[] modifiers = null) :
29 | base(metaData, name, modifiers ?? new[] {Modifier.Private})
30 | {
31 | Expression = expression ?? new NullExpression(MetaData);
32 | Type = type;
33 | Mutability = isMutable;
34 | }
35 |
36 | public override void SurroundWith(Environment environment)
37 | {
38 | base.SurroundWith(environment);
39 | // https://github.com/Cm-lang/Cm-Document/issues/12
40 | if (string.Equals(Name, ReservedWords.Recur, Ordinal))
41 | {
42 | Debug.Assert(null != Expression.Env);
43 | Type = Expression.GetExpressionType();
44 | return;
45 | }
46 | Expression.SurroundWith(Env);
47 | var exprType = Expression.GetExpressionType();
48 | // FEATURE #8
49 | Type = Type ?? exprType;
50 | // FEATURE #30
51 | Type.SurroundWith(Env);
52 | if (Type is UnknownType unknownType) Type = unknownType.Resolve();
53 | // FEATURE #11
54 | if (!string.Equals(exprType.ToString(), PrimaryType.NullType, Ordinal) &&
55 | !Equals(Type, exprType))
56 | // FEATURE #9
57 | Errors.Add($"{MetaData.GetErrorHeader()}type mismatch, expected: {Type}, actual: {exprType}");
58 | }
59 |
60 | public override void ConvertGoto() => Expression.ConvertGoto();
61 |
62 | ///
63 | /// Conservatism inline
64 | ///
65 | /// if it should be inlined
66 | public bool ShouldBeInlinedImmediately() =>
67 | Mutability && (UsageCount <= 1 || Expression is AtomicExpression);
68 |
69 | public override bool Equals(object obj) =>
70 | obj is Declaration declaration && declaration.Name == Name;
71 |
72 | public override IEnumerable Dump() => new[]
73 | {
74 | $"{(Mutability ? "mutable" : "immutable")} variable declaration [{Name}]:\n",
75 | " type:\n"
76 | }
77 | .Concat(Type?.Dump().Select(MapFunc2) ?? new[] {" cannot infer!\n"})
78 | .Concat(new[] {" initialize expression:\n"})
79 | .Concat(Expression.Dump().Select(MapFunc2));
80 |
81 | public override IEnumerable DumpCode() => new[]
82 | {
83 | $@"{(Mutability ? "var" : "let")} {Name}: {string.Join("", Type.DumpCode())} = {
84 | string.Join("", Expression.DumpCode())
85 | };
86 | "
87 | };
88 | }
89 | }
--------------------------------------------------------------------------------
/Cmc/Error.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using Cmc.Core;
4 | using JetBrains.Annotations;
5 |
6 | namespace Cmc
7 | {
8 | ///
9 | /// errors
10 | ///
11 | public static class Errors
12 | {
13 | [NotNull] public static IList ErrList = new List();
14 | [NotNull] public static Predicate Remove = ErrList.Remove;
15 |
16 | public static void Add(string s)
17 | {
18 | ErrList.Add(s);
19 | if (Pragma.AbortAtFirst) throw new CompilerException("aborted.");
20 | }
21 |
22 | public static void AddAndThrow(string s)
23 | {
24 | Add(s);
25 | throw new CompilerException(s);
26 | }
27 |
28 | public static void PrintErrorInfo() => Console.WriteLine(string.Join("\n", ErrList));
29 | }
30 |
31 | ///
32 | /// warnings
33 | ///
34 | public static class Warnings
35 | {
36 | [NotNull] public static IList WarnList = new List();
37 | [NotNull] public static Action Add = WarnList.Add;
38 | [NotNull] public static Predicate Remove = WarnList.Remove;
39 |
40 | public static void PrintErrorInfo() => Console.WriteLine(string.Join("\n", WarnList));
41 | }
42 |
43 | public class CompilerException : Exception
44 | {
45 | public CompilerException(
46 | [NotNull] string message = "compilation aborted due to fatal errors.") : base(message)
47 | {
48 | }
49 | }
50 | }
--------------------------------------------------------------------------------
/Cmc/Expr/AssignmentExpression.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using Cmc.Core;
5 | using Cmc.Decl;
6 | using Cmc.Expr;
7 | using JetBrains.Annotations;
8 | using Environment = Cmc.Core.Environment;
9 |
10 | namespace Cmc.Stmt
11 | {
12 | public class AssignmentExpression : Expression
13 | {
14 | [NotNull] public readonly Expression LhsExpression;
15 | [NotNull] public readonly Expression RhsExpression;
16 |
17 | public AssignmentExpression(
18 | MetaData metaData,
19 | [NotNull] Expression lhsExpression,
20 | [NotNull] Expression rhsExpression) :
21 | base(metaData)
22 | {
23 | LhsExpression = lhsExpression;
24 | RhsExpression = rhsExpression;
25 | }
26 |
27 | public override void SurroundWith(Environment environment)
28 | {
29 | base.SurroundWith(environment);
30 | LhsExpression.SurroundWith(Env);
31 | RhsExpression.SurroundWith(Env);
32 | var lhs = LhsExpression.GetExpressionType();
33 | var rhs = RhsExpression.GetExpressionType();
34 | // FEATURE #14
35 | // FEATURE #11
36 | if (!string.Equals(rhs.ToString(), PrimaryType.NullType, StringComparison.Ordinal) &&
37 | !string.Equals(lhs.ToString(), rhs.ToString(), StringComparison.Ordinal))
38 | Errors.Add($"{MetaData.GetErrorHeader()}assigning a {rhs} to a {lhs} is invalid.");
39 | // FEATURE #20
40 | var validLhs = LhsExpression.GetLhsExpression();
41 | if (null == validLhs)
42 | Errors.Add($"{MetaData.GetErrorHeader()}a {lhs} cannot be assigned.");
43 | else if (null == validLhs.Declaration)
44 | Errors.Add($"{MetaData.GetErrorHeader()}can't find declaration of {validLhs.Name}");
45 | // FEATURE #21
46 | else if (!validLhs.DeclarationMutability)
47 | Errors.Add($"{MetaData.GetErrorHeader()}cannot assign to an immutable variable.");
48 | else validLhs.Declaration.UsageCount++;
49 | if (!(RhsExpression is AtomicExpression))
50 | {
51 | // TODO
52 | // ConvertedStatementList = new StatementList(MetaData,
53 | // new VariableDeclaration(MetaData, ""));
54 | }
55 | }
56 |
57 | public override IEnumerable Dump() => new[]
58 | {
59 | "assignment statemnt:\n",
60 | " lhs:\n"
61 | }
62 | .Concat(LhsExpression.Dump().Select(MapFunc2))
63 | .Concat(new[] {" rhs:\n"})
64 | .Concat(RhsExpression.Dump().Select(MapFunc2));
65 |
66 | public override IEnumerable DumpCode() => new[]
67 | {$"{string.Join("", LhsExpression.DumpCode())} = {string.Join("", RhsExpression.DumpCode())}"};
68 |
69 | public override Type GetExpressionType() => new PrimaryType(MetaData, "void");
70 | }
71 | }
--------------------------------------------------------------------------------
/Cmc/Expr/ConstantPool.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 |
3 | namespace Cmc.Expr
4 | {
5 | ///
6 | /// FEATURE #38
7 | ///
8 | public static class Constants
9 | {
10 | public static readonly IList StringConstants = new List();
11 | public static readonly IList StringConstantLengths = new List();
12 |
13 | public static int AllocateStringConstant(string value, int len)
14 | {
15 | var index = StringConstants.IndexOf(value);
16 | if (-1 != index) return index;
17 | StringConstants.Add(value);
18 | StringConstantLengths.Add(len);
19 | return StringConstants.Count - 1;
20 | }
21 | }
22 | }
--------------------------------------------------------------------------------
/Cmc/Expr/Expression.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using Cmc.Core;
5 | using Cmc.Stmt;
6 | using JetBrains.Annotations;
7 | using Environment = Cmc.Core.Environment;
8 |
9 | #pragma warning disable 659
10 |
11 | namespace Cmc.Expr
12 | {
13 | public class ExpressionConvertedResult
14 | {
15 | [NotNull] public IList ConvertedStatements;
16 | [NotNull] public Expression ConvertedExpression;
17 |
18 | public ExpressionConvertedResult(
19 | [NotNull] IList convertedStatements,
20 | [NotNull] Expression convertedExpression)
21 | {
22 | ConvertedStatements = convertedStatements;
23 | ConvertedExpression = convertedExpression;
24 | }
25 | }
26 |
27 | public abstract class Expression : Ast
28 | {
29 | protected Expression(MetaData metaData) : base(metaData)
30 | {
31 | }
32 |
33 | [CanBeNull] public ExpressionConvertedResult ConvertedResult = null;
34 |
35 | [NotNull]
36 | public abstract Type GetExpressionType();
37 |
38 | [CanBeNull]
39 | public virtual VariableExpression GetLhsExpression() => null;
40 | }
41 |
42 | public sealed class NullExpression : AtomicExpression
43 | {
44 | public NullExpression(MetaData metaData) : base(metaData)
45 | {
46 | }
47 |
48 | public override Type GetExpressionType() => new PrimaryType(MetaData, PrimaryType.NullType);
49 |
50 | public override IEnumerable Dump() => new[] {"null expression\n"};
51 | public override IEnumerable DumpCode() => new[] {"null"};
52 | }
53 |
54 | ///
55 | /// expressions that can be used directly
56 | /// without turning it into a variable
57 | ///
58 | public abstract class AtomicExpression : Expression
59 | {
60 | protected AtomicExpression(MetaData metaData) : base(metaData)
61 | {
62 | }
63 | }
64 |
65 | public class StringLiteralExpression : Expression
66 | {
67 | public readonly int ConstantPoolIndex;
68 | public readonly int Length;
69 | [NotNull] public readonly string Value;
70 |
71 | [NotNull] public static readonly Type Type =
72 | new PrimaryType(MetaData.Empty, PrimaryType.StringType);
73 |
74 | public StringLiteralExpression(
75 | MetaData metaData,
76 | [NotNull] string value) :
77 | base(metaData)
78 | {
79 | Length = value.Length + 1;
80 | Value = string.Concat(
81 | from i in value
82 | select char.IsLetterOrDigit(i)
83 | ? i.ToString()
84 | : $"\\{Convert.ToString((byte) i, 16)}") + "\\00";
85 | ConstantPoolIndex = Constants.AllocateStringConstant(Value, Length);
86 | }
87 |
88 | ///
89 | ///
90 | /// FEATURE #23
91 | ///
92 | /// dump msg
93 | public override IEnumerable Dump() =>
94 | new[] {$"string literal expression <{ConstantPoolIndex}>[{Value}]:\n"}
95 | .Concat(Type.Dump().Select(MapFunc));
96 |
97 | public override IEnumerable DumpCode() => new[] {$"l\"{Value}\""};
98 |
99 | public override Type GetExpressionType() => Type;
100 | }
101 |
102 | public class MemberAccessExpression : Expression
103 | {
104 | [NotNull] public readonly VariableExpression Member;
105 | [NotNull] public readonly Expression Owner;
106 |
107 | public MemberAccessExpression(
108 | MetaData metaData,
109 | [NotNull] Expression owner,
110 | [NotNull] VariableExpression member) :
111 | base(metaData)
112 | {
113 | Owner = owner;
114 | Member = member;
115 | }
116 |
117 | public override void SurroundWith(Environment environment)
118 | {
119 | base.SurroundWith(environment);
120 | Owner.SurroundWith(Env);
121 | Member.SurroundWith(Env);
122 | // FEATURE #29
123 | if (!(Owner.GetExpressionType() is SecondaryType type) ||
124 | !type.Struct.FieldList.Contains(Member.Declaration))
125 | Errors.Add(MetaData.GetErrorHeader() + "invalid member access expression");
126 | if (null != Owner.ConvertedResult)
127 | {
128 | }
129 | }
130 |
131 | public override Type GetExpressionType() => Member.GetExpressionType();
132 |
133 | public override VariableExpression GetLhsExpression() => Member.GetLhsExpression();
134 |
135 | public override IEnumerable Dump() => new[]
136 | {
137 | "member access:\n",
138 | " owner:\n"
139 | }
140 | .Concat(Owner.Dump().Select(MapFunc2))
141 | .Concat(new[] {" member:\n"})
142 | .Concat(Member.Dump().Select(MapFunc2));
143 |
144 | public override IEnumerable DumpCode() =>
145 | new[] {$"{string.Join("", Owner.DumpCode())}.{string.Join("", Member)}"};
146 | }
147 |
148 | ///
149 | /// Idris-style hole-oriented programming
150 | ///
151 | public sealed class HoleExpression : Expression
152 | {
153 | public HoleExpression(MetaData metaData) : base(metaData)
154 | {
155 | }
156 |
157 | public override Type GetExpressionType() =>
158 | throw new CompilerException("cannot get hole's type");
159 |
160 | public override IEnumerable Dump() => new[] {"[H O L E]\n"};
161 | public override IEnumerable DumpCode() => new[] {"<>"};
162 | }
163 | }
--------------------------------------------------------------------------------
/Cmc/Expr/FunctionCallExpression.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using System.Linq;
3 | using Cmc.Core;
4 | using Cmc.Decl;
5 | using Cmc.Stmt;
6 | using JetBrains.Annotations;
7 | using static System.StringComparison;
8 |
9 | namespace Cmc.Expr
10 | {
11 | public class FunctionCallExpression : Expression
12 | {
13 | [NotNull] public readonly IList ArgsList;
14 | [NotNull] public readonly Expression Receiver;
15 | private Type _type;
16 |
17 | public FunctionCallExpression(
18 | MetaData metaData,
19 | [NotNull] Expression receiver,
20 | [NotNull] IList parameterList) :
21 | base(metaData)
22 | {
23 | Receiver = receiver;
24 | ArgsList = parameterList;
25 | }
26 |
27 | public override void SurroundWith(Environment environment)
28 | {
29 | base.SurroundWith(environment);
30 | foreach (var expression in ArgsList)
31 | expression.SurroundWith(environment);
32 | Receiver.SurroundWith(Env);
33 | // FEATURE #33
34 | if (Receiver is VariableExpression receiver)
35 | {
36 | var argsTypes = (from i in ArgsList
37 | select i.GetExpressionType())
38 | .ToList();
39 | var receiverDeclaration = Env.FindDeclarationSatisfies(declaration =>
40 | string.Equals(declaration.Name, receiver.Name, Ordinal) &&
41 | ((declaration is VariableDeclaration variableDeclaration &&
42 | variableDeclaration.Type is LambdaType lambdaType &&
43 | lambdaType.ParamsList.Count == ArgsList.Count &&
44 | lambdaType.ParamsList.SequenceEqual(argsTypes)) ||
45 | (declaration is ExternDeclaration externDeclaration &&
46 | externDeclaration.Type is LambdaType lambdaType2 &&
47 | lambdaType2.ParamsList.Count == ArgsList.Count &&
48 | lambdaType2.ParamsList.SequenceEqual(argsTypes))));
49 | if (null != receiverDeclaration)
50 | // receiverDeclaration is obviously a variable declaraion / extern declaration
51 | // because it's one of the filter condition
52 | receiver.Declaration = receiverDeclaration;
53 | else
54 | Errors.Add($"{MetaData.GetErrorHeader()}unresolved reference: \"{receiver.Name}\"");
55 | }
56 | LambdaType hisType;
57 | try
58 | {
59 | hisType = Receiver.GetExpressionType() as LambdaType;
60 | }
61 | catch (CompilerException)
62 | {
63 | if (Receiver is VariableExpression variable && string.Equals(variable.Name, "recur", Ordinal))
64 | throw new CompilerException(
65 | $"{MetaData.GetErrorHeader()}please specify lamdba return type when using `recur`");
66 | throw;
67 | }
68 | if (null != hisType)
69 | {
70 | _type = hisType.RetType;
71 | // FEATURE #32
72 | for (var i = 0; i < ArgsList.Count; i++)
73 | if (!Equals(ArgsList[i].GetExpressionType(), hisType.ParamsList[i]))
74 | Errors.Add(
75 | $"{MetaData.GetErrorHeader()}type mismatch: expected {hisType.ParamsList[i]}, " +
76 | $"found {ArgsList[i].GetExpressionType()}");
77 | }
78 | else
79 | Errors.Add(
80 | $"{MetaData.GetErrorHeader()}the function call receiver shoule be a function," +
81 | $" not {Receiver.GetExpressionType()}.");
82 | var tmp = Split();
83 | // if keepall, don't inline anything
84 | if (Pragma.KeepAll)
85 | {
86 | if (null != tmp)
87 | ConvertedResult = new ExpressionConvertedResult(tmp, this);
88 | return;
89 | }
90 | Inline(tmp);
91 | }
92 |
93 | private void Inline([CanBeNull] List tmp)
94 | {
95 | var statements = tmp ?? new List();
96 | // FEATURE #44
97 | if (Receiver is LambdaExpression lambdaExpression)
98 | {
99 | if (lambdaExpression.Recur) return;
100 | var statementList = lambdaExpression.OptimizedStatementList;
101 | var s = statementList?.Statements.ToList() ?? lambdaExpression.Body.Statements.ToList();
102 | var len = ArgsList.Count;
103 | // len = lambdaExpression.ParameterList.Count;
104 | // should equal
105 | for (var i = 0; i < len; i++)
106 | {
107 | var decl = lambdaExpression.ParameterList[i];
108 | decl.Mutability = true;
109 | decl.Expression = ArgsList[i];
110 | // decl.Type = ArgsList[i].GetExpressionType();
111 | statements.Add(decl);
112 | }
113 | Expression ret = null;
114 | for (var i = s.Count - 1; i >= 0; i--)
115 | if (s[i] is ReturnStatement returnStatement)
116 | {
117 | ret = returnStatement.Expression;
118 | s.RemoveAt(i);
119 | break;
120 | }
121 | ret = ret ?? new NullExpression(MetaData);
122 | if (!(ret is AtomicExpression))
123 | {
124 | var varName = $"retTmp{(ulong) ret.GetHashCode()}";
125 | var expr = new VariableDeclaration(MetaData, varName, ret, type: ret.GetExpressionType());
126 | s.Add(expr);
127 | var variableExpression = new VariableExpression(MetaData, varName)
128 | {
129 | Declaration = expr
130 | };
131 | ret = variableExpression;
132 | }
133 | statements.AddRange(s);
134 | ConvertedResult = new ExpressionConvertedResult(statements, ret);
135 | }
136 | else
137 | ConvertedResult = new ExpressionConvertedResult(statements, this);
138 | }
139 |
140 | public override void ConvertGoto() => Receiver.ConvertGoto();
141 |
142 | private IEnumerable DumpParams() => new[]
143 | {" parameters:\n"}
144 | .Concat(
145 | from i in ArgsList
146 | from j in i.Dump().Select(MapFunc2)
147 | select j);
148 |
149 | ///
150 | /// FEATURE #42
151 | /// if argument expressions are not atomic,
152 | /// convert them into seperate expressions
153 | ///
154 | private List Split()
155 | {
156 | List statements = null;
157 | for (var index = 0; index < ArgsList.Count; index++)
158 | {
159 | var expression = ArgsList[index];
160 | if (expression is AtomicExpression) continue;
161 | var name = $"tmp{(ulong) expression.GetHashCode()}";
162 | if (null == statements) statements = new List();
163 | VariableDeclaration decl;
164 | if (null == expression.ConvertedResult)
165 | // FEATURE #43
166 | decl = new VariableDeclaration(MetaData, name, expression)
167 | {
168 | Type = expression.GetExpressionType()
169 | };
170 | else
171 | {
172 | var convertedRes = expression.ConvertedResult;
173 | statements.AddRange(convertedRes.ConvertedStatements);
174 | decl = new VariableDeclaration(MetaData, name, convertedRes.ConvertedExpression)
175 | {
176 | Type = convertedRes.ConvertedExpression.GetExpressionType()
177 | };
178 | }
179 | statements.Add(decl);
180 | ArgsList[index] = new VariableExpression(MetaData, name)
181 | {
182 | Declaration = decl
183 | };
184 | }
185 | return statements;
186 | }
187 |
188 | public override Type GetExpressionType() =>
189 | _type ?? throw new CompilerException("type cannot be inferred");
190 |
191 | public override IEnumerable Dump() => new[]
192 | {
193 | "function call expression:\n",
194 | " receiver:\n"
195 | }
196 | .Concat(Receiver.Dump().Select(MapFunc2))
197 | .Concat(DumpParams())
198 | .Concat(new[] {" type:\n"})
199 | .Concat(_type.Dump().Select(MapFunc2));
200 |
201 | public override IEnumerable DumpCode() =>
202 | new[]
203 | {
204 | $"{string.Join("", Receiver.DumpCode())}({string.Join(",", ArgsList.Select(i => string.Join("", i.DumpCode())))})"
205 | };
206 | }
207 | }
--------------------------------------------------------------------------------
/Cmc/Expr/IfExpression.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using Cmc.Core;
5 | using Cmc.Expr;
6 | using JetBrains.Annotations;
7 | using Environment = Cmc.Core.Environment;
8 |
9 | namespace Cmc.Stmt
10 | {
11 | public class IfExpression : ConditionalExpression
12 | {
13 | [NotNull] public StatementList IfStatementList;
14 | [NotNull] public StatementList ElseStatementList;
15 | public int Optimized;
16 |
17 | public IfExpression(
18 | MetaData metaData,
19 | [NotNull] Expression condition,
20 | [NotNull] StatementList ifStatementList,
21 | [CanBeNull] StatementList elseStatementList = null) :
22 | base(metaData, condition)
23 | {
24 | IfStatementList = ifStatementList;
25 | ElseStatementList = elseStatementList ?? new StatementList(MetaData);
26 | }
27 |
28 | public override void SurroundWith(Environment environment)
29 | {
30 | base.SurroundWith(environment);
31 | // FEATURE #1
32 | var conditionType = Condition.GetExpressionType().ToString();
33 | if (!string.Equals(conditionType, PrimaryType.BoolType, StringComparison.Ordinal))
34 | Errors.Add(
35 | $"{MetaData.GetErrorHeader()}expected a bool as the \"if\" statement\'s condition, found {conditionType}");
36 | IfStatementList.SurroundWith(new Environment(Env));
37 | ElseStatementList.SurroundWith(new Environment(Env));
38 | // FEATURE #17
39 | if (Pragma.KeepAll || !(Condition is BoolLiteralExpression boolean)) return;
40 | if (boolean.Value)
41 | {
42 | ElseStatementList.Statements = new List(0);
43 | Optimized = 1;
44 | }
45 | else
46 | {
47 | IfStatementList.Statements = new List(0);
48 | Optimized = 2;
49 | }
50 | }
51 |
52 | public override void ConvertGoto()
53 | {
54 | IfStatementList.ConvertGoto();
55 | ElseStatementList.ConvertGoto();
56 | }
57 |
58 | public override IEnumerable Dump() => new[]
59 | {
60 | "if statement:\n",
61 | " condition:\n"
62 | }
63 | .Concat(Condition.Dump().Select(MapFunc2))
64 | .Concat(new[] {$" true branch{(Optimized == 2 ? " [optimized]" : "")}:\n"})
65 | .Concat(IfStatementList.Dump().Select(MapFunc2))
66 | .Concat(new[] {$" false branch{(Optimized == 1 ? " [optimized]" : "")}:\n"})
67 | .Concat(ElseStatementList.Dump().Select(MapFunc2));
68 |
69 | public override IEnumerable DumpCode() =>
70 | new[] {$"if ({string.Join("", Condition.DumpCode())}) {{\n"}
71 | .Concat(IfStatementList.DumpCode().Select(MapFunc))
72 | .Append("} else {\n")
73 | .Concat(ElseStatementList.DumpCode().Select(MapFunc))
74 | .Append("}");
75 |
76 | public override Type GetExpressionType() => new PrimaryType(MetaData, "void");
77 | }
78 | }
--------------------------------------------------------------------------------
/Cmc/Expr/LambdaExpression.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using System.Diagnostics;
3 | using System.Linq;
4 | using System.Runtime.InteropServices;
5 | using Cmc.Core;
6 | using Cmc.Decl;
7 | using Cmc.Stmt;
8 | using JetBrains.Annotations;
9 |
10 | namespace Cmc.Expr
11 | {
12 | ///
13 | /// A function is a variable with the type of lambda
14 | /// This is the class for anonymous lambda
15 | ///
16 | public class LambdaExpression : Expression
17 | {
18 | [CanBeNull] public Type DeclaredType;
19 | public readonly bool Recur;
20 | [NotNull] public StatementList Body;
21 | [NotNull] public readonly IList ParameterList;
22 | [NotNull] public readonly ReturnLabelDeclaration EndLabel;
23 | protected LambdaType Type;
24 |
25 | public LambdaExpression(
26 | MetaData metaData,
27 | [NotNull] StatementList body,
28 | // FEATURE #22
29 | [CanBeNull] IList parameterList = null,
30 | [CanBeNull] Type returnType = null,
31 | [CanBeNull] ReturnLabelDeclaration endLabel = null,
32 | bool recur = false) : base(metaData)
33 | {
34 | Body = body;
35 | DeclaredType = returnType;
36 | Recur = recur;
37 | ParameterList = parameterList ?? new List(0);
38 | EndLabel = endLabel ?? new ReturnLabelDeclaration(MetaData, "");
39 | }
40 |
41 | public override void SurroundWith(Environment environment)
42 | {
43 | base.SurroundWith(environment);
44 | if (DeclaredType is UnknownType unknownType)
45 | {
46 | unknownType.SurroundWith(Env);
47 | DeclaredType = unknownType.Resolve();
48 | }
49 | foreach (var variableDeclaration in ParameterList)
50 | variableDeclaration.SurroundWith(Env);
51 | EndLabel.SurroundWith(Env);
52 | var bodyEnv = new Environment(Env);
53 | Env.Declarations.Add(EndLabel);
54 | foreach (var variableDeclaration in ParameterList)
55 | bodyEnv.Declarations.Add(variableDeclaration);
56 | // https://github.com/Cm-lang/Cm-Document/issues/12
57 | if (null != DeclaredType)
58 | Type = new LambdaType(MetaData, (
59 | from i in ParameterList
60 | select i.Type).ToList(), DeclaredType);
61 | if (Recur)
62 | {
63 | // FEATURE #37
64 | var recur = new VariableDeclaration(MetaData, ReservedWords.Recur, this);
65 | // FEATURE #39
66 | recur.SurroundWith(Env);
67 | bodyEnv.Declarations.Add(recur);
68 | }
69 | Body.SurroundWith(bodyEnv);
70 | // while (null != Body.OptimizedStatementList)
71 | // Body = Body.OptimizedStatementList;
72 | while (null != Body.ConvertedStatementList)
73 | Body = Body.ConvertedStatementList;
74 | Body.Statements.Add(EndLabel);
75 | var retTypes = EndLabel.StatementsUsingThis.Select(i =>
76 | {
77 | i.ReturnLabel = EndLabel;
78 | return i.Expression.GetExpressionType();
79 | }).ToList();
80 | // Body.Flatten();
81 | if (retTypes.Any(i => !Equals(i, DeclaredType ?? retTypes.First())))
82 | Errors.Add(
83 | $"{MetaData.GetErrorHeader()}ambiguous return types:\n" +
84 | (DeclaredType != null ? $"<{DeclaredType}>" : "") +
85 | $"[{string.Join(",", from i in retTypes select i.ToString())}]");
86 | // FEATURE #12
87 | var retType = DeclaredType ?? (retTypes.Count != 0
88 | ? retTypes.First()
89 | // FEATURE #19
90 | : new PrimaryType(MetaData, PrimaryType.NullType));
91 | // FEATURE #46
92 | // FEATURE #47
93 | if (retTypes.Count > 1)
94 | {
95 | var varName = $"retClctor{(ulong) GetHashCode()}";
96 | Body.Statements.Insert(0, new VariableDeclaration(MetaData, varName, type: retType));
97 | var returnValueCollector = new VariableExpression(MetaData, varName);
98 | foreach (var endLabelStatement in EndLabel.StatementsUsingThis)
99 | endLabelStatement.Unify(returnValueCollector);
100 | Body.Statements.Add(new ReturnStatement(MetaData, returnValueCollector)
101 | {
102 | ReturnLabel = EndLabel
103 | });
104 | }
105 | Type = new LambdaType(MetaData, (
106 | from i in ParameterList
107 | select i.Type).ToList(), retType);
108 | }
109 |
110 | public override Type GetExpressionType() => Type;
111 | public override void ConvertGoto() => Body.ConvertGoto();
112 |
113 | public override IEnumerable Dump() => new[]
114 | {
115 | "lambda:\n",
116 | " type:\n"
117 | }
118 | .Concat(GetExpressionType().Dump().Select(MapFunc2))
119 | .Concat(new[] {" parameters:\n"})
120 | .Concat(
121 | from i in ParameterList
122 | from j in i.Dump().Select(MapFunc2)
123 | select j)
124 | .Concat(new[] {" body:\n"})
125 | .Concat(Body.Dump().Select(MapFunc2));
126 |
127 | public override IEnumerable DumpCode() => new[]
128 | {
129 | $@"{string.Join("", Type.RetType.DumpCode())} {(Recur ? "@" : "")}{{ {
130 | string.Join(", ",
131 | from param in ParameterList
132 | select $"{param.Name}: {string.Join("", param.Type.DumpCode())}")
133 | } ->
134 | "
135 | }
136 | .Concat(Body.DumpCode().Select(MapFunc))
137 | .Append("}");
138 | }
139 | }
--------------------------------------------------------------------------------
/Cmc/Expr/LiteralExpression.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using System.Linq;
3 | using Cmc.Core;
4 | using JetBrains.Annotations;
5 |
6 | namespace Cmc.Expr
7 | {
8 | public abstract class LiteralExpression : AtomicExpression
9 | {
10 | [NotNull] public readonly Type Type;
11 |
12 | protected LiteralExpression(
13 | MetaData metaData,
14 | [NotNull] Type type) :
15 | base(metaData) => Type = type;
16 |
17 | public override Type GetExpressionType() => Type;
18 | }
19 |
20 | public class IntLiteralExpression : LiteralExpression
21 | {
22 | public static readonly int[] AcceptableLength = {8, 16, 32, 64};
23 | public readonly int Length;
24 |
25 | [NotNull] public readonly string Value;
26 |
27 | public IntLiteralExpression(
28 | MetaData metaData,
29 | [NotNull] string value,
30 | // FEATURE #27
31 | bool isSigned,
32 | int length = 32)
33 | : base(metaData, new PrimaryType(metaData, $"{(isSigned ? "i" : "u")}{length}", length / 8))
34 | {
35 | Value = value;
36 | Length = length;
37 | // FEATURE #26
38 | if (!AcceptableLength.Contains(length))
39 | Errors.Add($"{MetaData.GetErrorHeader()}integer length of {length} is not supported");
40 | }
41 |
42 | public override IEnumerable Dump() =>
43 | new[] {$"int literal [{Value}]:\n"}
44 | .Concat(Type.Dump().Select(MapFunc));
45 |
46 | public override IEnumerable DumpCode() => new[] {$"{Value}{Type}"};
47 | }
48 |
49 | public class FloatLiteralExpression : LiteralExpression
50 | {
51 | public static readonly int[] AcceptableLength = {32, 64};
52 | public readonly int Length;
53 |
54 | [NotNull] public readonly string Value;
55 |
56 | public FloatLiteralExpression(
57 | MetaData metaData,
58 | [NotNull] string value,
59 | int length = 32)
60 | : base(metaData, new PrimaryType(metaData, $"f{length}", length / 8))
61 | {
62 | Value = value;
63 | Length = length;
64 | // FEATURE #41
65 | if (!AcceptableLength.Contains(length))
66 | Errors.Add($"{MetaData.GetErrorHeader()}float length of {length} is not supported");
67 | }
68 |
69 | public override IEnumerable Dump() =>
70 | new[] {$"float literal [{Value}]:\n"}
71 | .Concat(Type.Dump().Select(MapFunc));
72 |
73 | public override IEnumerable DumpCode() => new[] {$"{Value}{Type}"};
74 | }
75 |
76 | public class BoolLiteralExpression : LiteralExpression
77 | {
78 | public readonly bool Value;
79 |
80 | public BoolLiteralExpression(
81 | MetaData metaData,
82 | bool value) :
83 | base(metaData, new PrimaryType(
84 | metaData,
85 | PrimaryType.BoolType,
86 | 1))
87 | => Value = value;
88 |
89 | public int ValueToInt() => Value ? 1 : 0;
90 |
91 | public override IEnumerable Dump() =>
92 | new[] {$"bool literal expression [{Value}]:\n"};
93 |
94 | public override IEnumerable DumpCode() => new[] {$"{Value}"};
95 | }
96 | }
--------------------------------------------------------------------------------
/Cmc/Expr/VariableExpression.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using System.Linq;
3 | using Cmc.Core;
4 | using Cmc.Decl;
5 | using JetBrains.Annotations;
6 |
7 | namespace Cmc.Expr
8 | {
9 | public class VariableExpression : AtomicExpression
10 | {
11 | [NotNull] public readonly string Name;
12 |
13 | [CanBeNull]
14 | public Declaration Declaration
15 | {
16 | get => _declaration;
17 | set
18 | {
19 | switch (value)
20 | {
21 | case VariableDeclaration variable:
22 | ChangeDeclaration(variable);
23 | break;
24 | case ExternDeclaration @extern:
25 | ChangeDeclaration(@extern);
26 | break;
27 | default:
28 | Errors.AddAndThrow($"{MetaData.GetErrorHeader()}{value} isn't a variable");
29 | throw new CompilerException("gg");
30 | }
31 | }
32 | }
33 |
34 | public bool DeclarationMutability;
35 | [CanBeNull] private Type _declarationType;
36 | [CanBeNull] private Declaration _declaration;
37 |
38 | public VariableExpression(
39 | MetaData metaData,
40 | [NotNull] string name) :
41 | base(metaData) => Name = name;
42 |
43 | public override void SurroundWith(Environment environment)
44 | {
45 | base.SurroundWith(environment);
46 | var declaration = Env.FindDeclarationByName(Name);
47 | Declaration = declaration;
48 | }
49 |
50 | private void ChangeDeclaration(VariableDeclaration variableDeclaration)
51 | {
52 | _declarationType = variableDeclaration.Type;
53 | DeclarationMutability = variableDeclaration.Mutability;
54 | _declaration = variableDeclaration;
55 | _declaration.UsageCount++;
56 | }
57 |
58 | private void ChangeDeclaration(ExternDeclaration externDeclaration)
59 | {
60 | _declarationType = externDeclaration.Type;
61 | DeclarationMutability = externDeclaration.Mutability;
62 | _declaration = externDeclaration;
63 | _declaration.UsageCount++;
64 | }
65 |
66 | public override Type GetExpressionType() =>
67 | _declarationType ?? throw new CompilerException("unknown type");
68 |
69 | public override IEnumerable Dump() => new[]
70 | {
71 | $"variable expression [{Name}]:\n",
72 | " type:\n"
73 | }
74 | .Concat(_declarationType?.Dump().Select(MapFunc2) ?? new[] {" cannot infer!\n"});
75 |
76 | public override VariableExpression GetLhsExpression() => this;
77 |
78 | public override IEnumerable DumpCode() => new[] {$"{Name}"};
79 | }
80 | }
--------------------------------------------------------------------------------
/Cmc/Expr/WhileExpression.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using Cmc.Core;
5 | using Cmc.Decl;
6 | using Cmc.Expr;
7 | using JetBrains.Annotations;
8 | using Environment = Cmc.Core.Environment;
9 |
10 | namespace Cmc.Stmt
11 | {
12 | public abstract class ConditionalExpression : Expression
13 | {
14 | [NotNull] public readonly Expression Condition;
15 |
16 | protected ConditionalExpression(
17 | MetaData metaData,
18 | [NotNull] Expression condition) : base(metaData)
19 | {
20 | Condition = condition;
21 | }
22 |
23 | public override void SurroundWith(Environment environment)
24 | {
25 | base.SurroundWith(environment);
26 | Condition.SurroundWith(Env);
27 | }
28 | }
29 |
30 | public class WhileExpression : ConditionalExpression
31 | {
32 | [NotNull] public StatementList OkStatementList;
33 | public int Optimized;
34 | [NotNull] public readonly JumpLabelDeclaration EndLabel;
35 |
36 | public WhileExpression(
37 | MetaData metaData,
38 | [NotNull] Expression condition,
39 | [NotNull] StatementList okStatementList,
40 | [CanBeNull] JumpLabelDeclaration endLabel = null) :
41 | base(metaData, condition)
42 | {
43 | OkStatementList = okStatementList;
44 | EndLabel = endLabel ?? new JumpLabelDeclaration(MetaData, "");
45 | }
46 |
47 | public override void SurroundWith(Environment environment)
48 | {
49 | base.SurroundWith(environment);
50 | var jmp = new JumpLabelDeclaration(MetaData, "");
51 | jmp.SurroundWith(Env);
52 | EndLabel.SurroundWith(Env);
53 | // FEATURE #16
54 | var conditionType = Condition.GetExpressionType().ToString();
55 | if (!string.Equals(conditionType, PrimaryType.BoolType, StringComparison.Ordinal))
56 | Errors.Add(
57 | $"{MetaData.GetErrorHeader()}expected a bool as the \"while\" statement\'s condition, " +
58 | $"found {conditionType}");
59 | OkStatementList.Statements.Add(jmp);
60 | var bodyEnv = new Environment(Env);
61 | bodyEnv.Declarations.Add(EndLabel);
62 | OkStatementList.SurroundWith(bodyEnv);
63 | // FEATURE #17
64 | if (Pragma.KeepAll || !(Condition is BoolLiteralExpression boolean) || boolean.Value) return;
65 | OptimizedStatementList = new StatementList(MetaData);
66 | Optimized = 1;
67 | }
68 |
69 | public override void ConvertGoto() => OkStatementList.ConvertGoto();
70 |
71 | public override IEnumerable Dump() => new[]
72 | {
73 | "while statement:\n",
74 | " condition:\n"
75 | }
76 | .Concat(Condition.Dump().Select(MapFunc2))
77 | .Concat(new[] {" body:\n"})
78 | .Concat(Optimized == 1
79 | ? new[] {" [optimized]"}
80 | : OkStatementList.Dump().Select(MapFunc2));
81 |
82 | public override IEnumerable DumpCode() =>
83 | new[] {$"while ({string.Join("", Condition.DumpCode())}) {{\n"}
84 | .Concat(OkStatementList.DumpCode().Select(MapFunc))
85 | .Append("}");
86 |
87 | public override Type GetExpressionType() => new PrimaryType(MetaData, "void");
88 | }
89 | }
--------------------------------------------------------------------------------
/Cmc/Modifier.cs:
--------------------------------------------------------------------------------
1 | namespace Cmc
2 | {
3 | public enum Modifier
4 | {
5 | Private,
6 | Public,
7 | Operator,
8 | GcPointer, // pointer with garbage collection
9 | Inline // under discussion
10 | }
11 | }
--------------------------------------------------------------------------------
/Cmc/Stmt/ReturnStatement.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using System.Linq;
3 | using Cmc.Core;
4 | using Cmc.Decl;
5 | using Cmc.Expr;
6 | using JetBrains.Annotations;
7 |
8 | namespace Cmc.Stmt
9 | {
10 | ///
11 | /// when codegen, use `converted statement`
12 | ///
13 | public class ReturnStatement : ExpressionStatement
14 | {
15 | public ReturnLabelDeclaration ReturnLabel;
16 | [CanBeNull] private readonly string _labelName;
17 | public VariableDeclaration ConvertedVariableDeclaration;
18 | public ReturnStatement ConvertedReturnStatement;
19 |
20 | public ReturnStatement(
21 | MetaData metaData,
22 | [CanBeNull] Expression expression = null,
23 | [CanBeNull] string labelName = null) :
24 | base(metaData, expression ?? new NullExpression(metaData))
25 | {
26 | _labelName = labelName;
27 | }
28 |
29 | public override void SurroundWith(Environment environment)
30 | {
31 | // base.SurroundWith(environment);
32 | Env = environment;
33 | Expression.SurroundWith(Env);
34 | if (null != ReturnLabel) return;
35 | var returnLabel = Env.FindReturnLabelByName(_labelName ?? "");
36 | if (null == returnLabel)
37 | Errors.AddAndThrow($"{MetaData.GetErrorHeader()}cannot return outside a lambda");
38 | else
39 | {
40 | ReturnLabel = returnLabel;
41 | ReturnLabel.StatementsUsingThis.Add(this);
42 | }
43 | if (Expression is AtomicExpression) return;
44 | var variableName = $"genRet{(ulong) GetHashCode()}";
45 | ConvertedVariableDeclaration =
46 | new VariableDeclaration(MetaData, variableName, Expression, type: Expression.GetExpressionType());
47 | ConvertedReturnStatement = new ReturnStatement(MetaData,
48 | new VariableExpression(MetaData, variableName), _labelName)
49 | {
50 | ReturnLabel = ReturnLabel
51 | };
52 | ConvertedStatementList = new StatementList(MetaData,
53 | ConvertedVariableDeclaration,
54 | ConvertedReturnStatement);
55 | }
56 |
57 | ///
58 | /// FEATURE #45
59 | /// make this an inlined return statement
60 | ///
61 | /// the variable used to store the return value
62 | public void Unify([NotNull] VariableExpression returnValueStorer)
63 | {
64 | if (null != ConvertedStatementList)
65 | ConvertedStatementList = new StatementList(MetaData,
66 | ConvertedVariableDeclaration,
67 | new ExpressionStatement(MetaData,
68 | new AssignmentExpression(MetaData, returnValueStorer,
69 | ConvertedReturnStatement.Expression)),
70 | new GotoStatement(MetaData, ReturnLabel.GetLabel().Name));
71 | else
72 | ConvertedStatementList = new StatementList(MetaData,
73 | new ExpressionStatement(MetaData,
74 | new AssignmentExpression(MetaData, returnValueStorer, Expression)),
75 | new GotoStatement(MetaData, ReturnLabel.GetLabel().Name));
76 | }
77 |
78 | public override IEnumerable Dump() => new[]
79 | {$"return statement [{ReturnLabel}]:\n"}
80 | .Concat(Expression.Dump().Select(MapFunc));
81 |
82 | public override IEnumerable DumpCode() =>
83 | new[] {$"return:{ReturnLabel} {string.Join("", Expression.DumpCode())};\n"};
84 | }
85 |
86 | public class ExitStatement : Statement
87 | {
88 | [NotNull] public readonly AtomicExpression Expression;
89 |
90 | public ExitStatement(
91 | MetaData metaData,
92 | [NotNull] AtomicExpression expression) : base(metaData) => Expression = expression;
93 |
94 | public override IEnumerable Dump() => new[]
95 | {"return statement:\n"}
96 | .Concat(Expression.Dump().Select(MapFunc));
97 |
98 | public override IEnumerable DumpCode() =>
99 | new[] {$"return {string.Join("", Expression.DumpCode())};\n"};
100 | }
101 | }
--------------------------------------------------------------------------------
/Cmc/Stmt/Statement.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using System.Linq;
3 | using Cmc.Core;
4 | using Cmc.Decl;
5 | using Cmc.Expr;
6 | using JetBrains.Annotations;
7 | using Environment = Cmc.Core.Environment;
8 |
9 | namespace Cmc.Stmt
10 | {
11 | public class Statement : Ast
12 | {
13 | public Statement(MetaData metaData) : base(metaData)
14 | {
15 | }
16 |
17 | ///
18 | /// sometimes you need to convert those complex expressions
19 | /// or statements into a statement list.
20 | ///
21 | /// in order to express them as a list of simple expressions
22 | ///
23 | [CanBeNull] public StatementList ConvertedStatementList;
24 |
25 | public override IEnumerable Dump() => new[] {"empty statement"};
26 | public override IEnumerable DumpCode() => new[] {";\n"};
27 | }
28 |
29 | public class ExpressionStatement : Statement
30 | {
31 | [NotNull] public Expression Expression;
32 |
33 | public ExpressionStatement(
34 | MetaData metaData,
35 | [NotNull] Expression expression) :
36 | base(metaData)
37 | {
38 | Expression = expression;
39 | }
40 |
41 | public override void SurroundWith(Environment environment)
42 | {
43 | base.SurroundWith(environment);
44 | Expression.SurroundWith(Env);
45 | var res = Expression.ConvertedResult;
46 | if (null != res)
47 | ConvertedStatementList = new StatementList(MetaData,
48 | res.ConvertedStatements.Concat(new[]
49 | {
50 | new ExpressionStatement(MetaData, res.ConvertedExpression)
51 | }).ToArray());
52 | }
53 |
54 | public override void ConvertGoto() => Expression.ConvertGoto();
55 | public override IEnumerable DumpCode() => new[] {$"{string.Join("", Expression.DumpCode())};\n"};
56 |
57 | public override IEnumerable Dump() => new[] {"expression statement:\n"}
58 | .Concat(Expression.Dump().Select(MapFunc));
59 | }
60 |
61 | ///
62 | /// FEATURE #25
63 | ///
64 | public class JumpStatement : Statement
65 | {
66 | public enum Jump
67 | {
68 | Break,
69 | Continue
70 | }
71 |
72 | public readonly Jump JumpKind;
73 | public JumpLabelDeclaration JumpLabel;
74 | [CanBeNull] private readonly string _labelName;
75 |
76 | public JumpStatement(
77 | MetaData metaData,
78 | Jump jumpKind,
79 | [CanBeNull] string labelName = null) :
80 | base(metaData)
81 | {
82 | JumpKind = jumpKind;
83 | _labelName = labelName;
84 | }
85 |
86 | public override void SurroundWith(Environment environment)
87 | {
88 | base.SurroundWith(environment);
89 | var jumpLabel = Env.FindJumpLabelByName(_labelName ?? "");
90 | if (null == jumpLabel)
91 | Errors.AddAndThrow($"{MetaData.GetErrorHeader()}cannot return outside a lambda");
92 | else
93 | {
94 | JumpLabel = jumpLabel;
95 | JumpLabel.StatementsUsingThis.Add(this);
96 | }
97 | }
98 |
99 | public override string ToString() => JumpKind == Jump.Break ? "break" : "continue";
100 | public override IEnumerable Dump() => new[] {$"jump statement [{this}] [{JumpLabel}]\n"};
101 | public override IEnumerable DumpCode() => new[] {$"{this}:{JumpLabel};\n"};
102 | }
103 |
104 | public class GotoStatement : Statement
105 | {
106 | [NotNull] public readonly string Label;
107 |
108 | public GotoStatement(
109 | MetaData metaData,
110 | [NotNull] string label) : base(metaData) => Label = label;
111 |
112 | public override IEnumerable Dump() => new[] {$"goto statement [{Label}]:\n"};
113 | public override IEnumerable DumpCode() => new[] {$"goto {Label};\n"};
114 | }
115 | }
--------------------------------------------------------------------------------
/Cmc/Stmt/StatementList.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using System.Linq;
3 | using Cmc.Core;
4 | using Cmc.Decl;
5 | using Cmc.Expr;
6 | using JetBrains.Annotations;
7 |
8 | namespace Cmc.Stmt
9 | {
10 | public class StatementList : Statement
11 | {
12 | [NotNull] public List Statements;
13 | [NotNull] public List JumpOutStatements;
14 |
15 | public StatementList(
16 | MetaData metaData,
17 | params Statement[] statements) :
18 | base(metaData)
19 | {
20 | JumpOutStatements = new List();
21 | Statements = statements.ToList();
22 | }
23 |
24 | public StatementList(
25 | MetaData metaData,
26 | IEnumerable statements) :
27 | base(metaData)
28 | {
29 | JumpOutStatements = new List();
30 | Statements = statements.ToList();
31 | }
32 |
33 | public override void SurroundWith(Environment environment)
34 | {
35 | base.SurroundWith(environment);
36 | var env = new Environment(Env);
37 | var converted = new List(Statements.Count + 5);
38 | // FEATURE #4
39 | Statements.ForEach(statement =>
40 | {
41 | if (!(statement is Declaration declaration))
42 | statement.SurroundWith(env);
43 | else
44 | {
45 | statement.SurroundWith(env);
46 | env = new Environment(env);
47 | env.Declarations.Add(declaration);
48 | }
49 | if (statement is ExpressionStatement expression)
50 | {
51 | var convertedResult = expression.Expression.ConvertedResult;
52 | if (convertedResult != null && 0 != convertedResult.ConvertedStatements.Count)
53 | {
54 | converted.AddRange(convertedResult.ConvertedStatements);
55 | expression.Expression = convertedResult.ConvertedExpression;
56 | expression.ConvertedStatementList = null;
57 | converted.Add(expression);
58 | expression.Expression.ConvertedResult = null;
59 | // expression might be a return statement
60 | // converted.Add(new ExpressionStatement(MetaData, convertedResult.ConvertedExpression));
61 | }
62 | else
63 | converted.Add(expression);
64 | }
65 | else
66 | converted.Add(statement);
67 | });
68 | ConvertedStatementList = new StatementList(MetaData, converted);
69 | }
70 |
71 | ///
72 | /// FEATURE #45
73 | /// FEATURE #46
74 | /// FEATURE #47
75 | ///
76 | public override void ConvertGoto()
77 | {
78 | var res = new List();
79 | Statements.ForEach(statement =>
80 | {
81 | statement.ConvertGoto();
82 | switch (statement)
83 | {
84 | case ILabel label:
85 | res.Add(label.GetLabel());
86 | break;
87 | case JumpStatement jumpStatement:
88 | res.Add(new GotoStatement(jumpStatement.MetaData, $"{jumpStatement.JumpLabel}"));
89 | break;
90 | case ReturnStatement returnStatement:
91 | var converted = returnStatement.ConvertedStatementList;
92 | if (null != converted)
93 | res.AddRange(converted.Statements);
94 | else
95 | {
96 | var expr = returnStatement.Expression;
97 | if (!(expr is AtomicExpression atomic))
98 | {
99 | var varName = $"tmp{(ulong) returnStatement.GetHashCode()}";
100 | var varDecl = new VariableDeclaration(returnStatement.MetaData, varName, returnStatement.Expression)
101 | {
102 | Type = expr.GetExpressionType()
103 | };
104 | res.Add(varDecl);
105 | res.Add(new ExitStatement(returnStatement.MetaData, new VariableExpression(MetaData, varName)
106 | {
107 | Declaration = varDecl
108 | }));
109 | }
110 | else res.Add(new ExitStatement(returnStatement.MetaData, atomic));
111 | }
112 | break;
113 | default:
114 | res.Add(statement);
115 | break;
116 | }
117 | });
118 | Statements = res;
119 | }
120 |
121 | public override IEnumerable Dump() => Statements.Count == 0
122 | ? new[] {"empty statement list\n"}
123 | : new[] {"statement list:\n"}
124 | .Concat(
125 | from i in Statements
126 | from j in i.Dump().Select(MapFunc)
127 | select j);
128 |
129 | public override IEnumerable DumpCode() =>
130 | from stmt in Statements
131 | from codes in stmt.DumpCode()
132 | select codes;
133 | }
134 | }
--------------------------------------------------------------------------------
/Cmc/Type.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using System.Linq;
3 | using Cmc.Core;
4 | using Cmc.Decl;
5 | using JetBrains.Annotations;
6 | using static System.StringComparison;
7 | using Environment = Cmc.Core.Environment;
8 |
9 | #pragma warning disable 659
10 |
11 | namespace Cmc
12 | {
13 | public abstract class Type : Ast
14 | {
15 | [NotNull] public readonly string Name;
16 | public int Align;
17 |
18 | protected Type(
19 | MetaData metaData,
20 | [NotNull] string name,
21 | int align = 8) : base(metaData)
22 | {
23 | Name = name;
24 | Align = align;
25 | }
26 |
27 | public abstract override string ToString();
28 | public abstract override bool Equals([CanBeNull] object obj);
29 |
30 | public static IEnumerable FindCommon(IEnumerable list1, IEnumerable list2) =>
31 | list1.Where(list2.Contains).ToList();
32 | }
33 |
34 | public class UnknownType : Type
35 | {
36 | public UnknownType(MetaData metaData, [NotNull] string name) : base(metaData, name)
37 | {
38 | }
39 |
40 | ///
41 | /// FEATURE #30
42 | ///
43 | /// resolved type
44 | /// if unresolved
45 | [NotNull]
46 | public Type Resolve()
47 | {
48 | var declaration = Env.FindDeclarationByName(Name);
49 | switch (declaration)
50 | {
51 | case null:
52 | Gg();
53 | break;
54 | case TypeDeclaration typeDeclaration:
55 | typeDeclaration.UsageCount++;
56 | return typeDeclaration.Type;
57 | case StructDeclaration structDeclaration:
58 | structDeclaration.UsageCount++;
59 | return structDeclaration.Type;
60 | }
61 | var s = MetaData.GetErrorHeader() + Name + " is not a type";
62 | Errors.Add(s);
63 | throw new CompilerException(s);
64 | }
65 |
66 | public void Gg() =>
67 | Errors.Add($"{MetaData.GetErrorHeader()}unresolved type: {MetaData}");
68 |
69 | public override string ToString() => Name;
70 |
71 | public override bool Equals(object obj)
72 | {
73 | Gg();
74 | throw new CompilerException("unknown type");
75 | }
76 |
77 | public override IEnumerable Dump() => new[]
78 | {
79 | $"unknown type{Name}\n"
80 | };
81 |
82 | public override IEnumerable DumpCode() => new[] {$"[unknown:{Name}]"};
83 | }
84 |
85 | ///
86 | /// FEATURE #0
87 | ///
88 | public class PrimaryType : Type
89 | {
90 | [NotNull] public const string StringType = ReservedWords.StringType;
91 | [NotNull] public const string NullType = ReservedWords.NullType;
92 | [NotNull] public const string BoolType = ReservedWords.BoolType;
93 |
94 | public PrimaryType(
95 | MetaData metaData,
96 | [NotNull] string name) :
97 | base(metaData, name)
98 | {
99 | }
100 |
101 | public PrimaryType(
102 | MetaData metaData,
103 | [NotNull] string name,
104 | int align = 8) :
105 | base(metaData, name, align)
106 | {
107 | }
108 |
109 | public override IEnumerable Dump() => new[] {$"primary type [{this}]\n"};
110 | public override IEnumerable DumpCode() => new[] {$"{this}"};
111 |
112 | public override string ToString() => Name;
113 |
114 | public override bool Equals(object obj) =>
115 | obj is PrimaryType type && string.Equals(type.Name, Name, Ordinal);
116 | }
117 |
118 | ///
119 | /// FEATURE #7
120 | ///
121 | public class SecondaryType : Type
122 | {
123 | [CanBeNull] public StructDeclaration Struct;
124 |
125 | public SecondaryType(
126 | MetaData metaData,
127 | [NotNull] string name,
128 | [CanBeNull] StructDeclaration @struct = null) :
129 | base(metaData, name, 4)
130 | {
131 | Struct = @struct;
132 | }
133 |
134 | public override void SurroundWith(Environment environment)
135 | {
136 | base.SurroundWith(environment);
137 | if (null == Struct)
138 | Struct = Env.FindDeclarationByName(Name) as StructDeclaration;
139 | if (null == Struct)
140 | Errors.AddAndThrow($"{MetaData.GetErrorHeader()}cannot resolve type {Name}");
141 | else Struct.UsageCount++;
142 | }
143 |
144 | public override string ToString() => Name;
145 |
146 | public override bool Equals(object obj) =>
147 | obj is SecondaryType type && string.Equals(type.Name, Name, Ordinal);
148 |
149 | public override IEnumerable Dump() =>
150 | new[] {$"secondary type[{this}]:\n"};
151 |
152 | public override IEnumerable DumpCode() => new[] {$"{this}"};
153 | }
154 |
155 | ///
156 | /// FEATURE #6
157 | ///
158 | public class LambdaType : Type
159 | {
160 | [NotNull] public readonly IList ParamsList;
161 | [NotNull] public Type RetType;
162 |
163 | public LambdaType(MetaData metaData, [NotNull] IList @params, [NotNull] Type ret) :
164 | base(metaData, LambdaTypeToString(@params, ret))
165 | {
166 | ParamsList = @params;
167 | RetType = ret;
168 | }
169 |
170 | public override void SurroundWith(Environment environment)
171 | {
172 | base.SurroundWith(environment);
173 | for (var i = 0; i < ParamsList.Count; i++)
174 | {
175 | var type = ParamsList[i];
176 | type.SurroundWith(Env);
177 | if (type is UnknownType unknownType) ParamsList[i] = unknownType.Resolve();
178 | }
179 | RetType.SurroundWith(Env);
180 | if (RetType is UnknownType wtf) RetType = wtf.Resolve();
181 | }
182 |
183 | public override string ToString() => LambdaTypeToString(ParamsList, RetType);
184 |
185 | public override bool Equals(object obj)
186 | {
187 | var ok = obj is LambdaType type && Equals(type.RetType, RetType) &&
188 | Equals(type.ParamsList.Count, ParamsList.Count);
189 | if (!ok) return false;
190 | var lambdaType = (LambdaType) obj;
191 | return !ParamsList.Where((t, i) => !Equals(lambdaType.ParamsList[i], t)).Any();
192 | }
193 |
194 | [NotNull]
195 | public static string LambdaTypeToString([NotNull] IEnumerable args, [NotNull] Type ret) =>
196 | $"{ret}({string.Join(",", args)})";
197 |
198 | public override IEnumerable Dump() => new[]
199 | {$"lambda type [{this}]\n"};
200 |
201 | public override IEnumerable DumpCode() => new[] {$"{this}"};
202 | }
203 | }
--------------------------------------------------------------------------------
/CmcExec/CmcExec.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Debug
5 | AnyCPU
6 | {7172A461-5A1E-4297-B3F8-F4FC4192113D}
7 | {FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}
8 | Exe
9 | Properties
10 | CmcExec
11 | CmcExec
12 | netcoreapp2.0
13 | 512
14 |
15 |
16 |
17 | {17e0de41-2426-4616-8424-ba6c0f97f7c0}
18 | Cmc
19 |
20 |
21 | {f0f0ac0c-7ddb-4555-ad24-4724b14dbab5}
22 | LLVM
23 |
24 |
25 |
32 |
--------------------------------------------------------------------------------
/CmcExec/Executable.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using Cmc.Core;
3 | using Cmc.Decl;
4 | using Cmc.Expr;
5 | using Cmc.Stmt;
6 | using LLVM;
7 |
8 | namespace CmcExec
9 | {
10 | internal static class Executable
11 | {
12 | private static void TestOk() => Gen.RunLlvm(
13 | "main",
14 | "out.exe",
15 | new VariableDeclaration(MetaData.Empty,
16 | "i", new IntLiteralExpression(MetaData.Empty, "1", true)),
17 | new VariableDeclaration(MetaData.Empty,
18 | "j", new StringLiteralExpression(MetaData.Empty, "boy next door")),
19 | new VariableDeclaration(MetaData.Empty,
20 | "main", new LambdaExpression(MetaData.Empty,
21 | new StatementList(MetaData.Empty,
22 | new VariableDeclaration(MetaData.Empty,
23 | "j", new StringLiteralExpression(MetaData.Empty, "Hello, World")),
24 | new ExpressionStatement(MetaData.Empty,
25 | new FunctionCallExpression(MetaData.Empty,
26 | new VariableExpression(MetaData.Empty, "print"),
27 | new List(new[]
28 | {
29 | new VariableExpression(MetaData.Empty, "j")
30 | }))),
31 | new ReturnStatement(MetaData.Empty,
32 | new IntLiteralExpression(MetaData.Empty, "0", true)))))
33 | );
34 |
35 | public static void Main(string[] args) => TestOk();
36 | }
37 | }
--------------------------------------------------------------------------------
/CmcTest/CmcTest.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 | netcoreapp2.0
4 | false
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 | {17e0de41-2426-4616-8424-ba6c0f97f7c0}
15 | Cmc
16 |
17 |
18 |
--------------------------------------------------------------------------------
/CmcTest/DumpCodeTest.cs:
--------------------------------------------------------------------------------
1 | using Cmc;
2 | using Cmc.Core;
3 | using Cmc.Expr;
4 | using Microsoft.VisualStudio.TestTools.UnitTesting;
5 | using static CmcTest.FunctionCallTests;
6 | using static CmcTest.ReturnTests;
7 | using static CmcTest.StatementTests;
8 |
9 | namespace CmcTest
10 | {
11 | [TestClass]
12 | public class DumpCodeTest
13 | {
14 | [TestInitialize]
15 | public void Init()
16 | {
17 | Errors.ErrList.Clear();
18 | Pragma.KeepAll = false;
19 | Environment.Initialize();
20 | }
21 |
22 | [TestMethod]
23 | public void TestDump1()
24 | {
25 | var example = FuncCallAst1();
26 | example.SurroundWith(Environment.SolarSystem);
27 | example.ConvertGoto();
28 | example.PrintCode();
29 | }
30 |
31 | [TestMethod]
32 | public void TestDump2()
33 | {
34 | var example = FuncCallAst2();
35 | example.SurroundWith(Environment.SolarSystem);
36 | example.PrintCode();
37 | }
38 |
39 | [TestMethod]
40 | public void TestDump4()
41 | {
42 | var example = FuncCallAst4();
43 | example.SurroundWith(Environment.SolarSystem);
44 | example.PrintCode();
45 | }
46 |
47 | [TestMethod]
48 | public void TestDump5()
49 | {
50 | var example = FuncCallAst5();
51 | example.SurroundWith(Environment.SolarSystem);
52 | example.ConvertGoto();
53 | example.PrintCode();
54 | }
55 |
56 | [TestMethod]
57 | public void TestDump6()
58 | {
59 | var example = FuncCallAst6();
60 | example.SurroundWith(Environment.SolarSystem);
61 | example.ConvertGoto();
62 | example.PrintCode();
63 | }
64 |
65 | [TestMethod]
66 | public void TestDump5KeepAll()
67 | {
68 | Pragma.KeepAll = true;
69 | var example = FuncCallAst5();
70 | example.SurroundWith(Environment.SolarSystem);
71 | example.PrintCode();
72 | }
73 |
74 | [TestMethod]
75 | public void TestSplittingDump1() =>
76 | ExpressionSplittingTestCore(false,
77 | new IntLiteralExpression(MetaData.Empty, "123", true, 8),
78 | lambdaExpression =>
79 | {
80 | lambdaExpression.PrintCode();
81 | Assert.IsTrue(0 == Errors.ErrList.Count);
82 | });
83 |
84 | [TestMethod]
85 | public void TestSplittingDump2() =>
86 | ExpressionSplittingTestCore(true,
87 | new IntLiteralExpression(MetaData.Empty, "123", true, 8),
88 | lambdaExpression =>
89 | {
90 | lambdaExpression.PrintCode();
91 | Assert.IsTrue(0 == Errors.ErrList.Count);
92 | });
93 |
94 | [TestMethod]
95 | public void ReturnDumpTest1()
96 | {
97 | var block = Block1();
98 | block.SurroundWith(Environment.SolarSystem);
99 | block.ConvertGoto();
100 | block.PrintCode();
101 | }
102 |
103 | [TestMethod]
104 | public void ReturnDumpTest2()
105 | {
106 | var block = Block2();
107 | block.SurroundWith(Environment.SolarSystem);
108 | block.ConvertGoto();
109 | block.PrintCode();
110 | }
111 |
112 | [TestMethod]
113 | public void DumpStmtAst1()
114 | {
115 | var block = StmtAst1();
116 | block.SurroundWith(Environment.SolarSystem);
117 | block.PrintCode();
118 | }
119 |
120 | [TestMethod]
121 | public void DumpStmtAst2()
122 | {
123 | var block = StmtAst2();
124 | block.SurroundWith(Environment.SolarSystem);
125 | block.PrintCode();
126 | }
127 |
128 | [TestMethod]
129 | public void DumpStmtAst3()
130 | {
131 | var block = StmtAst3();
132 | block.SurroundWith(Environment.SolarSystem);
133 | block.ConvertGoto();
134 | block.PrintCode();
135 | }
136 |
137 | [TestMethod]
138 | public void DumpStmtAst4()
139 | {
140 | var block = StmtAst4();
141 | block.SurroundWith(Environment.SolarSystem);
142 | block.ConvertGoto();
143 | block.PrintCode();
144 | }
145 |
146 | [TestMethod]
147 | public void DumpStmtAst5()
148 | {
149 | var block = StmtAst5();
150 | block.SurroundWith(Environment.SolarSystem);
151 | block.PrintCode();
152 | }
153 |
154 | [TestMethod]
155 | public void DumpStmtAst6()
156 | {
157 | var block = StmtAst6();
158 | block.SurroundWith(Environment.SolarSystem);
159 | block.PrintCode();
160 | }
161 | }
162 | }
--------------------------------------------------------------------------------
/CmcTest/FunctionCallTests.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using Cmc;
4 | using Cmc.Core;
5 | using Cmc.Decl;
6 | using Cmc.Expr;
7 | using Cmc.Stmt;
8 | using Microsoft.VisualStudio.TestTools.UnitTesting;
9 | using Environment = Cmc.Core.Environment;
10 |
11 | namespace CmcTest
12 | {
13 | [TestClass]
14 | public class FunctionCallTests
15 | {
16 | ///
17 | /// id function
18 | /// let id = { a: i8 -> a }
19 | ///
20 | public static VariableDeclaration IdDeclaration() =>
21 | new VariableDeclaration(MetaData.Empty, "id",
22 | new LambdaExpression(MetaData.Empty,
23 | new StatementList(MetaData.Empty,
24 | new ReturnStatement(MetaData.Empty,
25 | new VariableExpression(MetaData.Empty, "a"))),
26 | new List(new[]
27 | {
28 | new VariableDeclaration(MetaData.Empty, "a", type:
29 | new UnknownType(MetaData.Empty, "i8"))
30 | })));
31 |
32 | public static StatementList FuncCallAst1() => new StatementList(MetaData.Empty,
33 | IdDeclaration(),
34 | new VariableDeclaration(MetaData.Empty, "gg", type:
35 | new UnknownType(MetaData.Empty, "i8")),
36 | new ExpressionStatement(MetaData.Empty, new AssignmentExpression(MetaData.Empty,
37 | new VariableExpression(MetaData.Empty, "gg"),
38 | new FunctionCallExpression(MetaData.Empty,
39 | new VariableExpression(MetaData.Empty, "id"),
40 | new List(new[] {new IntLiteralExpression(MetaData.Empty, "1", true)})))));
41 |
42 | public static StatementList FuncCallAst2() => new StatementList(MetaData.Empty,
43 | IdDeclaration(),
44 | new VariableDeclaration(MetaData.Empty, "gg", isMutable: true, type:
45 | new UnknownType(MetaData.Empty, "i8")),
46 | new ExpressionStatement(MetaData.Empty, new AssignmentExpression(MetaData.Empty,
47 | new VariableExpression(MetaData.Empty, "gg"),
48 | new FunctionCallExpression(MetaData.Empty,
49 | new VariableExpression(MetaData.Empty, "id"),
50 | new List(new[] {new IntLiteralExpression(MetaData.Empty, "233", true, 8)})))));
51 |
52 | public static LambdaExpression LambdaAst1() => new LambdaExpression(MetaData.Empty,
53 | new StatementList(MetaData.Empty,
54 | new ReturnStatement(MetaData.Empty,
55 | new FunctionCallExpression(MetaData.Empty,
56 | new VariableExpression(MetaData.Empty, "recur"),
57 | new List(new[]
58 | {
59 | new VariableExpression(MetaData.Empty, "a")
60 | })))),
61 | new List(new[]
62 | {
63 | new VariableDeclaration(MetaData.Empty, "a", type:
64 | new UnknownType(MetaData.Empty, "i8"))
65 | }));
66 |
67 | public static StatementList FuncCallAst3() => new StatementList(MetaData.Empty,
68 | new VariableDeclaration(MetaData.Empty, "recurFunc", LambdaAst1()),
69 | new VariableDeclaration(MetaData.Empty, "gg", isMutable: true, type:
70 | new UnknownType(MetaData.Empty, "i8")),
71 | new ExpressionStatement(MetaData.Empty, new AssignmentExpression(MetaData.Empty,
72 | new VariableExpression(MetaData.Empty, "gg"),
73 | new FunctionCallExpression(MetaData.Empty,
74 | new VariableExpression(MetaData.Empty, "recurFunc"),
75 | new List(new[]
76 | {
77 | new IntLiteralExpression(MetaData.Empty, "233", true, 8)
78 | })))));
79 |
80 | public static LambdaExpression LambdaAst2() => new LambdaExpression(MetaData.Empty,
81 | new StatementList(MetaData.Empty,
82 | new ReturnStatement(MetaData.Empty,
83 | new FunctionCallExpression(MetaData.Empty,
84 | new VariableExpression(MetaData.Empty, "recur"),
85 | new List(new[]
86 | {
87 | new VariableExpression(MetaData.Empty, "a")
88 | })))),
89 | new List(new[]
90 | {
91 | new VariableDeclaration(MetaData.Empty, "a", type:
92 | new UnknownType(MetaData.Empty, "i8"))
93 | }), new UnknownType(MetaData.Empty, "i8"),
94 | recur: true);
95 |
96 | public static StatementList FuncCallAst4() => new StatementList(MetaData.Empty,
97 | new VariableDeclaration(MetaData.Empty, "recurFunc", LambdaAst2()),
98 | new VariableDeclaration(MetaData.Empty, "gg", isMutable: true, type:
99 | new UnknownType(MetaData.Empty, "i8")),
100 | new ExpressionStatement(MetaData.Empty, new AssignmentExpression(MetaData.Empty,
101 | new VariableExpression(MetaData.Empty, "gg"),
102 | new FunctionCallExpression(MetaData.Empty,
103 | new VariableExpression(MetaData.Empty, "recurFunc"),
104 | new List(new[]
105 | {
106 | new IntLiteralExpression(MetaData.Empty, "233", true, 8)
107 | })))));
108 |
109 | public static LambdaExpression LambdaAst3() => new LambdaExpression(MetaData.Empty,
110 | new StatementList(MetaData.Empty,
111 | new ReturnStatement(MetaData.Empty,
112 | new FunctionCallExpression(MetaData.Empty,
113 | new LambdaExpression(MetaData.Empty,
114 | new StatementList(MetaData.Empty,
115 | new ReturnStatement(MetaData.Empty,
116 | new FunctionCallExpression(MetaData.Empty,
117 | new VariableExpression(MetaData.Empty, "recur"),
118 | new List(new[]
119 | {
120 | new VariableExpression(MetaData.Empty, "a")
121 | })),
122 | "inner")
123 | ), returnType:
124 | new UnknownType(MetaData.Empty, "i8"),
125 | endLabel: new ReturnLabelDeclaration(MetaData.Empty, "inner")),
126 | new List()))),
127 | new List(new[]
128 | {
129 | new VariableDeclaration(MetaData.Empty, "a", type:
130 | new UnknownType(MetaData.Empty, "i8"))
131 | }), new UnknownType(MetaData.Empty, "i8"),
132 | recur: true);
133 |
134 | public static LambdaExpression LambdaAst4() => new LambdaExpression(MetaData.Empty,
135 | new StatementList(MetaData.Empty,
136 | new ReturnStatement(MetaData.Empty,
137 | new FunctionCallExpression(MetaData.Empty,
138 | new LambdaExpression(MetaData.Empty,
139 | new StatementList(MetaData.Empty,
140 | new ReturnStatement(MetaData.Empty,
141 | new FunctionCallExpression(MetaData.Empty,
142 | new VariableExpression(MetaData.Empty, "recur"),
143 | new List(new[]
144 | {
145 | new VariableExpression(MetaData.Empty, "a")
146 | })),
147 | "inner")
148 | ),
149 | returnType: new UnknownType(MetaData.Empty, "i8"),
150 | parameterList: new List
151 | {
152 | new VariableDeclaration(MetaData.Empty, "unused_str",
153 | type: new PrimaryType(MetaData.Empty, "string"))
154 | },
155 | endLabel: new ReturnLabelDeclaration(MetaData.Empty, "inner")),
156 | new List
157 | {
158 | new StringLiteralExpression(MetaData.Empty, "ass we can")
159 | }))),
160 | new List(new[]
161 | {
162 | new VariableDeclaration(MetaData.Empty, "a", type:
163 | new UnknownType(MetaData.Empty, "i8"))
164 | }), new UnknownType(MetaData.Empty, "i8"),
165 | recur: true);
166 |
167 | public static StatementList FuncCallAst5() => new StatementList(MetaData.Empty,
168 | new VariableDeclaration(MetaData.Empty, "recurFunc", LambdaAst3()),
169 | new VariableDeclaration(MetaData.Empty, "gg", isMutable: true, type:
170 | new UnknownType(MetaData.Empty, "i8")),
171 | new ExpressionStatement(MetaData.Empty,
172 | new AssignmentExpression(MetaData.Empty,
173 | new VariableExpression(MetaData.Empty, "gg"),
174 | new FunctionCallExpression(MetaData.Empty,
175 | new VariableExpression(MetaData.Empty, "recurFunc"),
176 | new List(new[]
177 | {
178 | new IntLiteralExpression(MetaData.Empty, "233", true, 8)
179 | })))));
180 |
181 | public static StatementList FuncCallAst6() => new StatementList(MetaData.Empty,
182 | new VariableDeclaration(MetaData.Empty, "recurFuncPlus", LambdaAst4()),
183 | new VariableDeclaration(MetaData.Empty, "gg", isMutable: true, type:
184 | new UnknownType(MetaData.Empty, "i8")),
185 | new ExpressionStatement(MetaData.Empty, new AssignmentExpression(MetaData.Empty,
186 | new VariableExpression(MetaData.Empty, "gg"),
187 | new FunctionCallExpression(MetaData.Empty,
188 | new VariableExpression(MetaData.Empty, "recurFuncPlus"),
189 | new List(new[]
190 | {
191 | new IntLiteralExpression(MetaData.Empty, "233", true, 8)
192 | })))));
193 |
194 | [TestInitialize]
195 | public void Init()
196 | {
197 | Errors.ErrList.Clear();
198 | Pragma.KeepAll = false;
199 | Environment.Initialize();
200 | }
201 |
202 | [TestMethod]
203 | public void FuncCallTest1()
204 | {
205 | var example = FuncCallAst1();
206 | example.SurroundWith(Environment.SolarSystem);
207 | example.PrintDumpInfo();
208 | Console.WriteLine(string.Join("\n", Errors.ErrList));
209 | Assert.IsTrue(0 != Errors.ErrList.Count);
210 | }
211 |
212 | [TestMethod]
213 | public void FuncCallTest2()
214 | {
215 | var example = FuncCallAst2();
216 | example.SurroundWith(Environment.SolarSystem);
217 | example.PrintDumpInfo();
218 | Console.WriteLine(string.Join("\n", Errors.ErrList));
219 | Assert.IsTrue(0 == Errors.ErrList.Count);
220 | }
221 |
222 | ///
223 | /// recur fail test
224 | ///
225 | [TestMethod]
226 | public void FuncCallTest3() =>
227 | Assert.ThrowsException(() => { FuncCallAst3().SurroundWith(Environment.SolarSystem); });
228 |
229 | ///
230 | /// recur test
231 | ///
232 | [TestMethod]
233 | public void FuncCallTest4()
234 | {
235 | var example = FuncCallAst4();
236 | example.SurroundWith(Environment.SolarSystem);
237 | example.PrintDumpInfo();
238 | }
239 |
240 | ///
241 | /// recur test
242 | /// expression:
243 | /// const recurFunc = i8 { a: i32 -> i8 { recur(a) }() }
244 | /// var gg: i8 = null
245 | /// gg = recurFunc(233u8)
246 | ///
247 | [TestMethod]
248 | public void FuncCallTest5()
249 | {
250 | var example = FuncCallAst5();
251 | example.SurroundWith(Environment.SolarSystem);
252 | example.PrintDumpInfo();
253 | Errors.PrintErrorInfo();
254 | Assert.IsTrue(0 == Errors.ErrList.Count);
255 | }
256 |
257 | ///
258 | /// recur test
259 | /// expression:
260 | /// const recurFuncPlus = i8 { a: i32 -> i8 { unused_str: string -> recur(a) }("ass we can") }
261 | /// var gg: i8 = null
262 | /// gg = recurFuncPlus(233u8)
263 | ///
264 | [TestMethod]
265 | public void FuncCallTest6()
266 | {
267 | var example = FuncCallAst6();
268 | example.SurroundWith(Environment.SolarSystem);
269 | example.PrintDumpInfo();
270 | Errors.PrintErrorInfo();
271 | Assert.IsTrue(0 == Errors.ErrList.Count);
272 | }
273 |
274 | [TestMethod]
275 | public void FuncCallTest5KeelAll()
276 | {
277 | Pragma.KeepAll = true;
278 | var example = FuncCallAst5();
279 | example.SurroundWith(Environment.SolarSystem);
280 | example.PrintDumpInfo();
281 | Errors.PrintErrorInfo();
282 | Assert.IsTrue(0 == Errors.ErrList.Count);
283 | }
284 |
285 | public static void ExpressionSplittingTestCore(
286 | bool keepAll,
287 | IntLiteralExpression parameter,
288 | Action action)
289 | {
290 | Pragma.KeepAll = keepAll;
291 | var expr = new FunctionCallExpression(MetaData.Empty,
292 | new VariableExpression(MetaData.Empty, "id"),
293 | new List(new[]
294 | {
295 | new FunctionCallExpression(MetaData.Empty,
296 | new VariableExpression(MetaData.Empty, "id"),
297 | new List(new[]
298 | {
299 | new FunctionCallExpression(MetaData.Empty,
300 | new VariableExpression(MetaData.Empty, "id"),
301 | new List(new[] {parameter})
302 | )
303 | }))
304 | }));
305 | var core = new Core();
306 | var lambdaExpression = new LambdaExpression(MetaData.Empty,
307 | new StatementList(MetaData.Empty,
308 | new ReturnStatement(MetaData.Empty,
309 | expr)));
310 | core.Analyze(
311 | IdDeclaration(),
312 | new VariableDeclaration(MetaData.Empty, "_", lambdaExpression));
313 | // Assert.IsNotNull(expr.ConvertedResult);
314 | Errors.PrintErrorInfo();
315 | action(lambdaExpression);
316 | }
317 |
318 | [TestMethod]
319 | public void ExpressionSplittingTest1() =>
320 | ExpressionSplittingTestCore(false,
321 | new IntLiteralExpression(MetaData.Empty, "123", true),
322 | lambdaExpression =>
323 | {
324 | Assert.IsTrue(0 != Errors.ErrList.Count);
325 | lambdaExpression.Body.PrintDumpInfo();
326 | });
327 |
328 | [TestMethod]
329 | public void ExpressionSplittingTest2() =>
330 | ExpressionSplittingTestCore(false,
331 | new IntLiteralExpression(MetaData.Empty, "123", true, 8),
332 | lambdaExpression =>
333 | {
334 | lambdaExpression.Body.PrintDumpInfo();
335 | Assert.IsTrue(0 == Errors.ErrList.Count);
336 | });
337 |
338 | [TestMethod]
339 | public void ExpressionSplittingTest3() =>
340 | ExpressionSplittingTestCore(true,
341 | new IntLiteralExpression(MetaData.Empty, "123", true, 8),
342 | lambdaExpression =>
343 | {
344 | lambdaExpression.Body.PrintDumpInfo();
345 | Assert.IsTrue(0 == Errors.ErrList.Count);
346 | });
347 | }
348 | }
--------------------------------------------------------------------------------
/CmcTest/MutualRecTests.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using Cmc;
3 | using Cmc.Core;
4 | using Cmc.Decl;
5 | using Microsoft.VisualStudio.TestTools.UnitTesting;
6 |
7 | namespace CmcTest
8 | {
9 | [TestClass]
10 | public class MutualRecTests
11 | {
12 | public static StructDeclaration StructDef1() => new StructDeclaration(MetaData.Empty, "A",
13 | new List(new[]
14 | {
15 | new VariableDeclaration(MetaData.Empty, "var1", type:
16 | new UnknownType(MetaData.Empty, "A"))
17 | }));
18 |
19 | public static StructDeclaration StructDef2() => new StructDeclaration(MetaData.Empty, "A",
20 | new List(new[]
21 | {
22 | new VariableDeclaration(MetaData.Empty, "var1", type:
23 | new UnknownType(MetaData.Empty, "B")),
24 | new VariableDeclaration(MetaData.Empty, "var3", type:
25 | new UnknownType(MetaData.Empty, "u8"))
26 | }));
27 |
28 | public static StructDeclaration StructDef3() => new StructDeclaration(MetaData.Empty, "B",
29 | new List(new[]
30 | {
31 | new VariableDeclaration(MetaData.Empty, "var2", type:
32 | new UnknownType(MetaData.Empty, "A"))
33 | }));
34 |
35 | public static StructDeclaration[] StructDefs() => new[]
36 | {
37 | new StructDeclaration(MetaData.Empty, "A",
38 | new List(new[]
39 | {
40 | new VariableDeclaration(MetaData.Empty, "var1", type:
41 | new UnknownType(MetaData.Empty, "B")),
42 | new VariableDeclaration(MetaData.Empty, "var3", type:
43 | new UnknownType(MetaData.Empty, "u8"))
44 | })),
45 | new StructDeclaration(MetaData.Empty, "B",
46 | new List(new[]
47 | {
48 | new VariableDeclaration(MetaData.Empty, "var5", type:
49 | new UnknownType(MetaData.Empty, "C")),
50 | new VariableDeclaration(MetaData.Empty, "var5", type:
51 | new UnknownType(MetaData.Empty, "string")),
52 | new VariableDeclaration(MetaData.Empty, "var5", type:
53 | new UnknownType(MetaData.Empty, "nulltype"))
54 | })),
55 | new StructDeclaration(MetaData.Empty, "C",
56 | new List(new[]
57 | {
58 | new VariableDeclaration(MetaData.Empty, "var7", type:
59 | new UnknownType(MetaData.Empty, "D"))
60 | })),
61 | new StructDeclaration(MetaData.Empty, "D",
62 | new List(new[]
63 | {
64 | new VariableDeclaration(MetaData.Empty, "var9", type:
65 | new UnknownType(MetaData.Empty, "A"))
66 | }))
67 | };
68 |
69 | [TestInitialize]
70 | public void Init() => Errors.ErrList.Clear();
71 |
72 | [TestMethod]
73 | public void MutualRecTest1()
74 | {
75 | var core = new Core();
76 | var def = StructDef1();
77 | var env = new Environment(Environment.SolarSystem);
78 | env.Declarations.Add(def);
79 | core.CheckMutualRec(new[] {def});
80 | Assert.IsTrue(0 != Errors.ErrList.Count);
81 | Errors.PrintErrorInfo();
82 | }
83 |
84 | [TestMethod]
85 | public void MutualRecTest2()
86 | {
87 | var core = new Core();
88 | var def = StructDef2();
89 | var def2 = StructDef3();
90 | var env = new Environment(Environment.SolarSystem);
91 | env.Declarations.Add(def);
92 | env.Declarations.Add(def2);
93 | core.CheckMutualRec(new[] {def, def2});
94 | Assert.IsTrue(0 != Errors.ErrList.Count);
95 | Errors.PrintErrorInfo();
96 | }
97 |
98 | [TestMethod]
99 | public void MutualRecTest3()
100 | {
101 | var core = new Core();
102 | var defs = StructDefs();
103 | var env = new Environment(Environment.SolarSystem);
104 | foreach (var structDeclaration in defs)
105 | env.Declarations.Add(structDeclaration);
106 | // foreach (var structDeclaration in defs)
107 | // structDeclaration.SurroundWith(env);
108 | // foreach (var structDeclaration in defs)
109 | // structDeclaration.PrintDumpInfo();
110 | core.CheckMutualRec(defs);
111 | Assert.IsTrue(0 != Errors.ErrList.Count);
112 | Errors.PrintErrorInfo();
113 | }
114 | }
115 | }
--------------------------------------------------------------------------------
/CmcTest/ReturnTests.cs:
--------------------------------------------------------------------------------
1 | using Cmc;
2 | using Cmc.Core;
3 | using Cmc.Decl;
4 | using Cmc.Expr;
5 | using Cmc.Stmt;
6 | using Microsoft.VisualStudio.TestTools.UnitTesting;
7 |
8 | namespace CmcTest
9 | {
10 | [TestClass]
11 | public class ReturnTests
12 | {
13 | private const string Var = "var";
14 |
15 | public static LambdaExpression Block2() => new LambdaExpression(MetaData.Empty,
16 | new StatementList(MetaData.Empty,
17 | new VariableDeclaration(MetaData.Empty, Var,
18 | new BoolLiteralExpression(MetaData.Empty, false)),
19 | new ExpressionStatement(MetaData.Empty, new IfExpression(MetaData.Empty,
20 | new VariableExpression(MetaData.Empty, Var),
21 | new StatementList(MetaData.Empty,
22 | new ReturnStatement(MetaData.Empty,
23 | new IntLiteralExpression(MetaData.Empty, "23", false, 8))),
24 | new StatementList(MetaData.Empty,
25 | new ReturnStatement(MetaData.Empty,
26 | new IntLiteralExpression(MetaData.Empty, "45", false, 8)))))));
27 |
28 | public static LambdaExpression Block1() => new LambdaExpression(MetaData.Empty,
29 | new StatementList(MetaData.Empty,
30 | new VariableDeclaration(MetaData.Empty, Var,
31 | new BoolLiteralExpression(MetaData.Empty, false)),
32 | new ExpressionStatement(MetaData.Empty, new IfExpression(MetaData.Empty,
33 | new VariableExpression(MetaData.Empty, Var),
34 | new StatementList(MetaData.Empty,
35 | new ReturnStatement(MetaData.Empty,
36 | new IntLiteralExpression(MetaData.Empty, "0", false, 8))),
37 | new StatementList(MetaData.Empty,
38 | new ReturnStatement(MetaData.Empty,
39 | new IntLiteralExpression(MetaData.Empty, "1", true, 8)))))));
40 |
41 | [TestInitialize]
42 | public void Init() => Errors.ErrList.Clear();
43 |
44 | [TestMethod]
45 | public void ReturnTest1()
46 | {
47 | var block = Block1();
48 | block.SurroundWith(Environment.SolarSystem);
49 | block.PrintDumpInfo();
50 | Errors.PrintErrorInfo();
51 | Assert.IsTrue(0 != Errors.ErrList.Count);
52 | }
53 |
54 | [TestMethod]
55 | public void ReturnTest2()
56 | {
57 | var block = Block2();
58 | block.SurroundWith(Environment.SolarSystem);
59 | block.PrintDumpInfo();
60 | Assert.IsTrue(0 == Errors.ErrList.Count);
61 | }
62 | }
63 | }
--------------------------------------------------------------------------------
/CmcTest/StatementTests.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using Cmc;
3 | using Cmc.Core;
4 | using Cmc.Decl;
5 | using Cmc.Expr;
6 | using Cmc.Stmt;
7 | using Microsoft.VisualStudio.TestTools.UnitTesting;
8 | using Environment = Cmc.Core.Environment;
9 |
10 | namespace CmcTest
11 | {
12 | [TestClass]
13 | public class StatementTests
14 | {
15 | [TestInitialize]
16 | public void Init() => Errors.ErrList.Clear();
17 |
18 | private const string Var1 = "variableOne";
19 |
20 | public static Statement StmtAst3() => new StatementList(MetaData.Empty,
21 | new VariableDeclaration(MetaData.Empty, Var1,
22 | new BoolLiteralExpression(MetaData.Empty, true)),
23 | new ExpressionStatement(MetaData.Empty, new WhileExpression(MetaData.Empty,
24 | new VariableExpression(MetaData.Empty, Var1),
25 | new StatementList(MetaData.Empty,
26 | new ExpressionStatement(MetaData.Empty, new AssignmentExpression(MetaData.Empty,
27 | new VariableExpression(MetaData.Empty, Var1),
28 | new BoolLiteralExpression(MetaData.Empty, false)))))));
29 |
30 | public static Statement StmtAst4() => new StatementList(MetaData.Empty,
31 | new VariableDeclaration(MetaData.Empty, Var1,
32 | new BoolLiteralExpression(MetaData.Empty, true), true),
33 | new ExpressionStatement(MetaData.Empty, new WhileExpression(MetaData.Empty,
34 | new VariableExpression(MetaData.Empty, Var1),
35 | new StatementList(MetaData.Empty,
36 | new ExpressionStatement(MetaData.Empty, new AssignmentExpression(MetaData.Empty,
37 | new VariableExpression(MetaData.Empty, Var1),
38 | new BoolLiteralExpression(MetaData.Empty, false)))))));
39 |
40 | public static Statement StmtAst6() => new StatementList(MetaData.Empty,
41 | new VariableDeclaration(MetaData.Empty, Var1,
42 | new BoolLiteralExpression(MetaData.Empty, true), true),
43 | new ExpressionStatement(MetaData.Empty, new WhileExpression(MetaData.Empty,
44 | new VariableExpression(MetaData.Empty, Var1),
45 | new StatementList(MetaData.Empty,
46 | new ExpressionStatement(MetaData.Empty, new AssignmentExpression(MetaData.Empty,
47 | new VariableExpression(MetaData.Empty, Var1),
48 | new IntLiteralExpression(MetaData.Empty, "123", true)))))));
49 |
50 | public static Statement StmtAst5() => new StatementList(MetaData.Empty,
51 | new VariableDeclaration(MetaData.Empty, Var1,
52 | new BoolLiteralExpression(MetaData.Empty, true), true),
53 | new ExpressionStatement(MetaData.Empty, new WhileExpression(MetaData.Empty,
54 | new VariableExpression(MetaData.Empty, Var1),
55 | new StatementList(MetaData.Empty,
56 | new ExpressionStatement(MetaData.Empty, new AssignmentExpression(MetaData.Empty,
57 | new VariableExpression(MetaData.Empty, Var1),
58 | new NullExpression(MetaData.Empty)))))));
59 |
60 | public static Statement StmtAst2() => new ExpressionStatement(MetaData.Empty,
61 | new IfExpression(
62 | MetaData.Empty,
63 | new NullExpression(MetaData.Empty),
64 | new StatementList(MetaData.Empty)
65 | ));
66 |
67 | public static Statement StmtAst1() => new ExpressionStatement(MetaData.Empty,
68 | new IfExpression(
69 | MetaData.Empty,
70 | new BoolLiteralExpression(MetaData.Empty, false),
71 | new StatementList(MetaData.Empty)
72 | ));
73 |
74 | ///
75 | /// simplest test
76 | ///
77 | [TestMethod]
78 | public void StatementTest1()
79 | {
80 | foreach (var stmt in
81 | new StatementList(MetaData.Empty,
82 | new Statement(MetaData.Empty)).Statements)
83 | stmt.PrintDumpInfo();
84 | }
85 |
86 | ///
87 | /// check for condition type
88 | ///
89 | [TestMethod]
90 | public void StatementTest2()
91 | {
92 | var stmt = StmtAst1();
93 | stmt.SurroundWith(Environment.SolarSystem);
94 | stmt.PrintDumpInfo();
95 | Assert.IsTrue(0 == Errors.ErrList.Count);
96 | var stmt2 = StmtAst2();
97 | stmt2.SurroundWith(Environment.SolarSystem);
98 | Console.WriteLine("");
99 | Console.WriteLine("");
100 | Assert.IsTrue(0 != Errors.ErrList.Count);
101 | foreach (var s in Errors.ErrList)
102 | Console.WriteLine(s);
103 | stmt2.PrintDumpInfo();
104 | }
105 |
106 | ///
107 | /// check for mutability
108 | ///
109 | [TestMethod]
110 | public void StatementTest3()
111 | {
112 | var stmt = StmtAst3();
113 | stmt.SurroundWith(Environment.SolarSystem);
114 | stmt.PrintDumpInfo();
115 | Assert.IsTrue(0 != Errors.ErrList.Count);
116 | Errors.PrintErrorInfo();
117 | }
118 |
119 | ///
120 | /// type check (when it's correct)
121 | ///
122 | [TestMethod]
123 | public void StatementTest4()
124 | {
125 | var stmt = StmtAst4();
126 | stmt.SurroundWith(Environment.SolarSystem);
127 | stmt.PrintDumpInfo();
128 | Assert.IsTrue(0 == Errors.ErrList.Count);
129 | }
130 |
131 | ///
132 | /// type check (when it's nulltype)
133 | ///
134 | [TestMethod]
135 | public void StatementTest5()
136 | {
137 | var stmt = StmtAst5();
138 | stmt.SurroundWith(Environment.SolarSystem);
139 | stmt.PrintDumpInfo();
140 | Assert.IsTrue(0 == Errors.ErrList.Count);
141 | }
142 |
143 | ///
144 | /// type check (when it's incorrect)
145 | ///
146 | [TestMethod]
147 | public void StatementTest6()
148 | {
149 | var stmt = StmtAst6();
150 | stmt.SurroundWith(Environment.SolarSystem);
151 | stmt.PrintDumpInfo();
152 | Assert.IsTrue(0 != Errors.ErrList.Count);
153 | Errors.PrintErrorInfo();
154 | }
155 | }
156 | }
--------------------------------------------------------------------------------
/CmcTest/StructTests.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using System.Linq;
3 | using Cmc;
4 | using Cmc.Core;
5 | using Cmc.Decl;
6 | using Cmc.Expr;
7 | using Cmc.Stmt;
8 | using Microsoft.VisualStudio.TestTools.UnitTesting;
9 |
10 | namespace CmcTest
11 | {
12 | [TestClass]
13 | public class StructTests
14 | {
15 | [TestMethod]
16 | public void StructTest1()
17 | {
18 | var @struct = StructAst1();
19 | @struct.SurroundWith(Environment.SolarSystem);
20 | @struct.PrintDumpInfo();
21 | var a = (VariableDeclaration) @struct.Statements.Last();
22 | Assert.AreEqual("Person", a.Type.ToString());
23 | Assert.IsTrue(a.Type is SecondaryType);
24 | }
25 |
26 | public static StatementList StructAst1() => new StatementList(MetaData.Empty,
27 | new StructDeclaration(MetaData.Empty, "Person",
28 | new List(new[]
29 | {
30 | new VariableDeclaration(MetaData.Empty, "name",
31 | new StringLiteralExpression(MetaData.Empty, "ice")),
32 | new VariableDeclaration(MetaData.Empty, "gender",
33 | new IntLiteralExpression(MetaData.Empty, "123", false))
34 | })),
35 | new VariableDeclaration(MetaData.Empty, "var", type:
36 | new UnknownType(MetaData.Empty, "Person")));
37 | }
38 | }
--------------------------------------------------------------------------------
/CmcTest/TypeTests.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using Cmc;
5 | using Cmc.Core;
6 | using Cmc.Decl;
7 | using Cmc.Expr;
8 | using Cmc.Stmt;
9 | using Microsoft.VisualStudio.TestTools.UnitTesting;
10 | using Environment = Cmc.Core.Environment;
11 |
12 | namespace CmcTest
13 | {
14 | [TestClass]
15 | public class TypeTests
16 | {
17 | private const string VarName = "someVar";
18 |
19 | public static StatementList TypeInferenceAst1() => new StatementList(MetaData.Empty,
20 | new VariableDeclaration(MetaData.Empty, VarName,
21 | new IntLiteralExpression(MetaData.Empty, "123", false, 8)),
22 | new ExpressionStatement(MetaData.Empty,
23 | new VariableExpression(MetaData.Empty, VarName)));
24 |
25 | public static StatementList TypeInferenceAst2() => new StatementList(MetaData.Empty,
26 | new VariableDeclaration(MetaData.Empty, VarName,
27 | new NullExpression(MetaData.Empty)),
28 | new ExpressionStatement(MetaData.Empty,
29 | new VariableExpression(MetaData.Empty, VarName)));
30 |
31 | public static StatementList TypeInferenceAst3() => new StatementList(MetaData.Empty,
32 | new VariableDeclaration(MetaData.Empty, VarName,
33 | new NullExpression(MetaData.Empty),
34 | type: new UnknownType(MetaData.Empty, "i8")),
35 | new ExpressionStatement(MetaData.Empty,
36 | new VariableExpression(MetaData.Empty, VarName)));
37 |
38 | public static VariableDeclaration TypeInferenceAst4() => new VariableDeclaration(MetaData.Empty, VarName,
39 | new LambdaExpression(MetaData.Empty,
40 | new StatementList(MetaData.Empty,
41 | new ReturnStatement(MetaData.Empty,
42 | new IntLiteralExpression(MetaData.Empty, "0", true)))));
43 |
44 | [TestInitialize]
45 | public void Init()
46 | {
47 | Environment.Initialize();
48 | Errors.ErrList.Clear();
49 | }
50 |
51 | ///
52 | /// var someVar = 123u8;
53 | /// someVar; // the type of this expression will be inferred as "u8".
54 | ///
55 | [TestMethod]
56 | public void TypeInferenceTest1()
57 | {
58 | var example = TypeInferenceAst1();
59 | example.SurroundWith(Environment.SolarSystem);
60 | example.PrintDumpInfo();
61 | // ReSharper disable once PossibleNullReferenceException
62 | Assert.AreEqual("u8", ((ExpressionStatement) example.Statements.Last())
63 | .Expression.GetExpressionType().ToString());
64 | }
65 |
66 | ///
67 | /// var someVar = null;
68 | /// someVar; // nulltype
69 | ///
70 | [TestMethod]
71 | public void TypeInferenceTest2()
72 | {
73 | var example = TypeInferenceAst2();
74 | example.SurroundWith(Environment.SolarSystem);
75 | example.PrintDumpInfo();
76 | Assert.AreEqual(PrimaryType.NullType,
77 | ((ExpressionStatement) example.Statements.Last())
78 | .Expression.GetExpressionType().ToString());
79 | }
80 |
81 | ///
82 | /// var otherVar: i8 = null;
83 | /// otherVar; // i8
84 | /// FEATURE #11
85 | ///
86 | [TestMethod]
87 | public void TypeInferenceTest3()
88 | {
89 | var example = TypeInferenceAst3();
90 | example.SurroundWith(Environment.SolarSystem);
91 | Console.WriteLine(string.Join("", example.Dump()));
92 | Assert.AreEqual("i8", ((ExpressionStatement) example.Statements.Last())
93 | .Expression.GetExpressionType().ToString());
94 | }
95 |
96 | ///
97 | /// lambda type inference
98 | ///
99 | [TestMethod]
100 | public void TypeInferenceTest4()
101 | {
102 | var example = TypeInferenceAst4();
103 | example.SurroundWith(Environment.SolarSystem);
104 | example.PrintDumpInfo();
105 | var type = (LambdaType) example.Type;
106 | Assert.IsNotNull(type);
107 | Assert.AreEqual("i32", type.RetType.ToString());
108 | Assert.IsTrue(0 == type.ParamsList.Count);
109 | }
110 |
111 | [TestMethod]
112 | public void TypeTest1()
113 | {
114 | LiteralExpression example = new IntLiteralExpression(MetaData.Empty, "123456789", true, 64);
115 | example.PrintDumpInfo();
116 | Assert.AreEqual("i64", example.Type.ToString());
117 | example = new IntLiteralExpression(MetaData.Empty, "123456789", true);
118 | example.PrintDumpInfo();
119 | Assert.AreEqual("i32", example.Type.ToString());
120 | example = new IntLiteralExpression(MetaData.Empty, "123456789", false, 64);
121 | example.PrintDumpInfo();
122 | Assert.AreEqual("u64", example.Type.ToString());
123 | example = new IntLiteralExpression(MetaData.Empty, "123456789", false, 64);
124 | example.PrintDumpInfo();
125 | Assert.AreEqual("u64", example.Type.ToString());
126 | var example2 = new StringLiteralExpression(MetaData.Empty, "\"boy \\ next \\ door\n\t\"");
127 | example2.PrintDumpInfo();
128 | Assert.AreEqual(PrimaryType.StringType, example2.GetExpressionType().ToString());
129 | // var type = new SecondaryType(MetaData.Empty, "vec", new PrimaryType(MetaData.Empty, "i8"), example.Type);
130 | // type.SurroundWith(Environment.TopEnvironment);
131 | // type.PrintDumpInfo();
132 | }
133 | }
134 | }
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 3, 29 June 2007
3 |
4 | Copyright (C) 2007 Free Software Foundation, Inc.
5 | Everyone is permitted to copy and distribute verbatim copies
6 | of this license document, but changing it is not allowed.
7 |
8 | Preamble
9 |
10 | The GNU General Public License is a free, copyleft license for
11 | software and other kinds of works.
12 |
13 | The licenses for most software and other practical works are designed
14 | to take away your freedom to share and change the works. By contrast,
15 | the GNU General Public License is intended to guarantee your freedom to
16 | share and change all versions of a program--to make sure it remains free
17 | software for all its users. We, the Free Software Foundation, use the
18 | GNU General Public License for most of our software; it applies also to
19 | any other work released this way by its authors. You can apply it to
20 | your programs, too.
21 |
22 | When we speak of free software, we are referring to freedom, not
23 | price. Our General Public Licenses are designed to make sure that you
24 | have the freedom to distribute copies of free software (and charge for
25 | them if you wish), that you receive source code or can get it if you
26 | want it, that you can change the software or use pieces of it in new
27 | free programs, and that you know you can do these things.
28 |
29 | To protect your rights, we need to prevent others from denying you
30 | these rights or asking you to surrender the rights. Therefore, you have
31 | certain responsibilities if you distribute copies of the software, or if
32 | you modify it: responsibilities to respect the freedom of others.
33 |
34 | For example, if you distribute copies of such a program, whether
35 | gratis or for a fee, you must pass on to the recipients the same
36 | freedoms that you received. You must make sure that they, too, receive
37 | or can get the source code. And you must show them these terms so they
38 | know their rights.
39 |
40 | Developers that use the GNU GPL protect your rights with two steps:
41 | (1) assert copyright on the software, and (2) offer you this License
42 | giving you legal permission to copy, distribute and/or modify it.
43 |
44 | For the developers' and authors' protection, the GPL clearly explains
45 | that there is no warranty for this free software. For both users' and
46 | authors' sake, the GPL requires that modified versions be marked as
47 | changed, so that their problems will not be attributed erroneously to
48 | authors of previous versions.
49 |
50 | Some devices are designed to deny users access to install or run
51 | modified versions of the software inside them, although the manufacturer
52 | can do so. This is fundamentally incompatible with the aim of
53 | protecting users' freedom to change the software. The systematic
54 | pattern of such abuse occurs in the area of products for individuals to
55 | use, which is precisely where it is most unacceptable. Therefore, we
56 | have designed this version of the GPL to prohibit the practice for those
57 | products. If such problems arise substantially in other domains, we
58 | stand ready to extend this provision to those domains in future versions
59 | of the GPL, as needed to protect the freedom of users.
60 |
61 | Finally, every program is threatened constantly by software patents.
62 | States should not allow patents to restrict development and use of
63 | software on general-purpose computers, but in those that do, we wish to
64 | avoid the special danger that patents applied to a free program could
65 | make it effectively proprietary. To prevent this, the GPL assures that
66 | patents cannot be used to render the program non-free.
67 |
68 | The precise terms and conditions for copying, distribution and
69 | modification follow.
70 |
71 | TERMS AND CONDITIONS
72 |
73 | 0. Definitions.
74 |
75 | "This License" refers to version 3 of the GNU General Public License.
76 |
77 | "Copyright" also means copyright-like laws that apply to other kinds of
78 | works, such as semiconductor masks.
79 |
80 | "The Program" refers to any copyrightable work licensed under this
81 | License. Each licensee is addressed as "you". "Licensees" and
82 | "recipients" may be individuals or organizations.
83 |
84 | To "modify" a work means to copy from or adapt all or part of the work
85 | in a fashion requiring copyright permission, other than the making of an
86 | exact copy. The resulting work is called a "modified version" of the
87 | earlier work or a work "based on" the earlier work.
88 |
89 | A "covered work" means either the unmodified Program or a work based
90 | on the Program.
91 |
92 | To "propagate" a work means to do anything with it that, without
93 | permission, would make you directly or secondarily liable for
94 | infringement under applicable copyright law, except executing it on a
95 | computer or modifying a private copy. Propagation includes copying,
96 | distribution (with or without modification), making available to the
97 | public, and in some countries other activities as well.
98 |
99 | To "convey" a work means any kind of propagation that enables other
100 | parties to make or receive copies. Mere interaction with a user through
101 | a computer network, with no transfer of a copy, is not conveying.
102 |
103 | An interactive user interface displays "Appropriate Legal Notices"
104 | to the extent that it includes a convenient and prominently visible
105 | feature that (1) displays an appropriate copyright notice, and (2)
106 | tells the user that there is no warranty for the work (except to the
107 | extent that warranties are provided), that licensees may convey the
108 | work under this License, and how to view a copy of this License. If
109 | the interface presents a list of user commands or options, such as a
110 | menu, a prominent item in the list meets this criterion.
111 |
112 | 1. Source Code.
113 |
114 | The "source code" for a work means the preferred form of the work
115 | for making modifications to it. "Object code" means any non-source
116 | form of a work.
117 |
118 | A "Standard Interface" means an interface that either is an official
119 | standard defined by a recognized standards body, or, in the case of
120 | interfaces specified for a particular programming language, one that
121 | is widely used among developers working in that language.
122 |
123 | The "System Libraries" of an executable work include anything, other
124 | than the work as a whole, that (a) is included in the normal form of
125 | packaging a Major Component, but which is not part of that Major
126 | Component, and (b) serves only to enable use of the work with that
127 | Major Component, or to implement a Standard Interface for which an
128 | implementation is available to the public in source code form. A
129 | "Major Component", in this context, means a major essential component
130 | (kernel, window system, and so on) of the specific operating system
131 | (if any) on which the executable work runs, or a compiler used to
132 | produce the work, or an object code interpreter used to run it.
133 |
134 | The "Corresponding Source" for a work in object code form means all
135 | the source code needed to generate, install, and (for an executable
136 | work) run the object code and to modify the work, including scripts to
137 | control those activities. However, it does not include the work's
138 | System Libraries, or general-purpose tools or generally available free
139 | programs which are used unmodified in performing those activities but
140 | which are not part of the work. For example, Corresponding Source
141 | includes interface definition files associated with source files for
142 | the work, and the source code for shared libraries and dynamically
143 | linked subprograms that the work is specifically designed to require,
144 | such as by intimate data communication or control flow between those
145 | subprograms and other parts of the work.
146 |
147 | The Corresponding Source need not include anything that users
148 | can regenerate automatically from other parts of the Corresponding
149 | Source.
150 |
151 | The Corresponding Source for a work in source code form is that
152 | same work.
153 |
154 | 2. Basic Permissions.
155 |
156 | All rights granted under this License are granted for the term of
157 | copyright on the Program, and are irrevocable provided the stated
158 | conditions are met. This License explicitly affirms your unlimited
159 | permission to run the unmodified Program. The output from running a
160 | covered work is covered by this License only if the output, given its
161 | content, constitutes a covered work. This License acknowledges your
162 | rights of fair use or other equivalent, as provided by copyright law.
163 |
164 | You may make, run and propagate covered works that you do not
165 | convey, without conditions so long as your license otherwise remains
166 | in force. You may convey covered works to others for the sole purpose
167 | of having them make modifications exclusively for you, or provide you
168 | with facilities for running those works, provided that you comply with
169 | the terms of this License in conveying all material for which you do
170 | not control copyright. Those thus making or running the covered works
171 | for you must do so exclusively on your behalf, under your direction
172 | and control, on terms that prohibit them from making any copies of
173 | your copyrighted material outside their relationship with you.
174 |
175 | Conveying under any other circumstances is permitted solely under
176 | the conditions stated below. Sublicensing is not allowed; section 10
177 | makes it unnecessary.
178 |
179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
180 |
181 | No covered work shall be deemed part of an effective technological
182 | measure under any applicable law fulfilling obligations under article
183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or
184 | similar laws prohibiting or restricting circumvention of such
185 | measures.
186 |
187 | When you convey a covered work, you waive any legal power to forbid
188 | circumvention of technological measures to the extent such circumvention
189 | is effected by exercising rights under this License with respect to
190 | the covered work, and you disclaim any intention to limit operation or
191 | modification of the work as a means of enforcing, against the work's
192 | users, your or third parties' legal rights to forbid circumvention of
193 | technological measures.
194 |
195 | 4. Conveying Verbatim Copies.
196 |
197 | You may convey verbatim copies of the Program's source code as you
198 | receive it, in any medium, provided that you conspicuously and
199 | appropriately publish on each copy an appropriate copyright notice;
200 | keep intact all notices stating that this License and any
201 | non-permissive terms added in accord with section 7 apply to the code;
202 | keep intact all notices of the absence of any warranty; and give all
203 | recipients a copy of this License along with the Program.
204 |
205 | You may charge any price or no price for each copy that you convey,
206 | and you may offer support or warranty protection for a fee.
207 |
208 | 5. Conveying Modified Source Versions.
209 |
210 | You may convey a work based on the Program, or the modifications to
211 | produce it from the Program, in the form of source code under the
212 | terms of section 4, provided that you also meet all of these conditions:
213 |
214 | a) The work must carry prominent notices stating that you modified
215 | it, and giving a relevant date.
216 |
217 | b) The work must carry prominent notices stating that it is
218 | released under this License and any conditions added under section
219 | 7. This requirement modifies the requirement in section 4 to
220 | "keep intact all notices".
221 |
222 | c) You must license the entire work, as a whole, under this
223 | License to anyone who comes into possession of a copy. This
224 | License will therefore apply, along with any applicable section 7
225 | additional terms, to the whole of the work, and all its parts,
226 | regardless of how they are packaged. This License gives no
227 | permission to license the work in any other way, but it does not
228 | invalidate such permission if you have separately received it.
229 |
230 | d) If the work has interactive user interfaces, each must display
231 | Appropriate Legal Notices; however, if the Program has interactive
232 | interfaces that do not display Appropriate Legal Notices, your
233 | work need not make them do so.
234 |
235 | A compilation of a covered work with other separate and independent
236 | works, which are not by their nature extensions of the covered work,
237 | and which are not combined with it such as to form a larger program,
238 | in or on a volume of a storage or distribution medium, is called an
239 | "aggregate" if the compilation and its resulting copyright are not
240 | used to limit the access or legal rights of the compilation's users
241 | beyond what the individual works permit. Inclusion of a covered work
242 | in an aggregate does not cause this License to apply to the other
243 | parts of the aggregate.
244 |
245 | 6. Conveying Non-Source Forms.
246 |
247 | You may convey a covered work in object code form under the terms
248 | of sections 4 and 5, provided that you also convey the
249 | machine-readable Corresponding Source under the terms of this License,
250 | in one of these ways:
251 |
252 | a) Convey the object code in, or embodied in, a physical product
253 | (including a physical distribution medium), accompanied by the
254 | Corresponding Source fixed on a durable physical medium
255 | customarily used for software interchange.
256 |
257 | b) Convey the object code in, or embodied in, a physical product
258 | (including a physical distribution medium), accompanied by a
259 | written offer, valid for at least three years and valid for as
260 | long as you offer spare parts or customer support for that product
261 | model, to give anyone who possesses the object code either (1) a
262 | copy of the Corresponding Source for all the software in the
263 | product that is covered by this License, on a durable physical
264 | medium customarily used for software interchange, for a price no
265 | more than your reasonable cost of physically performing this
266 | conveying of source, or (2) access to copy the
267 | Corresponding Source from a network server at no charge.
268 |
269 | c) Convey individual copies of the object code with a copy of the
270 | written offer to provide the Corresponding Source. This
271 | alternative is allowed only occasionally and noncommercially, and
272 | only if you received the object code with such an offer, in accord
273 | with subsection 6b.
274 |
275 | d) Convey the object code by offering access from a designated
276 | place (gratis or for a charge), and offer equivalent access to the
277 | Corresponding Source in the same way through the same place at no
278 | further charge. You need not require recipients to copy the
279 | Corresponding Source along with the object code. If the place to
280 | copy the object code is a network server, the Corresponding Source
281 | may be on a different server (operated by you or a third party)
282 | that supports equivalent copying facilities, provided you maintain
283 | clear directions next to the object code saying where to find the
284 | Corresponding Source. Regardless of what server hosts the
285 | Corresponding Source, you remain obligated to ensure that it is
286 | available for as long as needed to satisfy these requirements.
287 |
288 | e) Convey the object code using peer-to-peer transmission, provided
289 | you inform other peers where the object code and Corresponding
290 | Source of the work are being offered to the general public at no
291 | charge under subsection 6d.
292 |
293 | A separable portion of the object code, whose source code is excluded
294 | from the Corresponding Source as a System Library, need not be
295 | included in conveying the object code work.
296 |
297 | A "User Product" is either (1) a "consumer product", which means any
298 | tangible personal property which is normally used for personal, family,
299 | or household purposes, or (2) anything designed or sold for incorporation
300 | into a dwelling. In determining whether a product is a consumer product,
301 | doubtful cases shall be resolved in favor of coverage. For a particular
302 | product received by a particular user, "normally used" refers to a
303 | typical or common use of that class of product, regardless of the status
304 | of the particular user or of the way in which the particular user
305 | actually uses, or expects or is expected to use, the product. A product
306 | is a consumer product regardless of whether the product has substantial
307 | commercial, industrial or non-consumer uses, unless such uses represent
308 | the only significant mode of use of the product.
309 |
310 | "Installation Information" for a User Product means any methods,
311 | procedures, authorization keys, or other information required to install
312 | and execute modified versions of a covered work in that User Product from
313 | a modified version of its Corresponding Source. The information must
314 | suffice to ensure that the continued functioning of the modified object
315 | code is in no case prevented or interfered with solely because
316 | modification has been made.
317 |
318 | If you convey an object code work under this section in, or with, or
319 | specifically for use in, a User Product, and the conveying occurs as
320 | part of a transaction in which the right of possession and use of the
321 | User Product is transferred to the recipient in perpetuity or for a
322 | fixed term (regardless of how the transaction is characterized), the
323 | Corresponding Source conveyed under this section must be accompanied
324 | by the Installation Information. But this requirement does not apply
325 | if neither you nor any third party retains the ability to install
326 | modified object code on the User Product (for example, the work has
327 | been installed in ROM).
328 |
329 | The requirement to provide Installation Information does not include a
330 | requirement to continue to provide support service, warranty, or updates
331 | for a work that has been modified or installed by the recipient, or for
332 | the User Product in which it has been modified or installed. Access to a
333 | network may be denied when the modification itself materially and
334 | adversely affects the operation of the network or violates the rules and
335 | protocols for communication across the network.
336 |
337 | Corresponding Source conveyed, and Installation Information provided,
338 | in accord with this section must be in a format that is publicly
339 | documented (and with an implementation available to the public in
340 | source code form), and must require no special password or key for
341 | unpacking, reading or copying.
342 |
343 | 7. Additional Terms.
344 |
345 | "Additional permissions" are terms that supplement the terms of this
346 | License by making exceptions from one or more of its conditions.
347 | Additional permissions that are applicable to the entire Program shall
348 | be treated as though they were included in this License, to the extent
349 | that they are valid under applicable law. If additional permissions
350 | apply only to part of the Program, that part may be used separately
351 | under those permissions, but the entire Program remains governed by
352 | this License without regard to the additional permissions.
353 |
354 | When you convey a copy of a covered work, you may at your option
355 | remove any additional permissions from that copy, or from any part of
356 | it. (Additional permissions may be written to require their own
357 | removal in certain cases when you modify the work.) You may place
358 | additional permissions on material, added by you to a covered work,
359 | for which you have or can give appropriate copyright permission.
360 |
361 | Notwithstanding any other provision of this License, for material you
362 | add to a covered work, you may (if authorized by the copyright holders of
363 | that material) supplement the terms of this License with terms:
364 |
365 | a) Disclaiming warranty or limiting liability differently from the
366 | terms of sections 15 and 16 of this License; or
367 |
368 | b) Requiring preservation of specified reasonable legal notices or
369 | author attributions in that material or in the Appropriate Legal
370 | Notices displayed by works containing it; or
371 |
372 | c) Prohibiting misrepresentation of the origin of that material, or
373 | requiring that modified versions of such material be marked in
374 | reasonable ways as different from the original version; or
375 |
376 | d) Limiting the use for publicity purposes of names of licensors or
377 | authors of the material; or
378 |
379 | e) Declining to grant rights under trademark law for use of some
380 | trade names, trademarks, or service marks; or
381 |
382 | f) Requiring indemnification of licensors and authors of that
383 | material by anyone who conveys the material (or modified versions of
384 | it) with contractual assumptions of liability to the recipient, for
385 | any liability that these contractual assumptions directly impose on
386 | those licensors and authors.
387 |
388 | All other non-permissive additional terms are considered "further
389 | restrictions" within the meaning of section 10. If the Program as you
390 | received it, or any part of it, contains a notice stating that it is
391 | governed by this License along with a term that is a further
392 | restriction, you may remove that term. If a license document contains
393 | a further restriction but permits relicensing or conveying under this
394 | License, you may add to a covered work material governed by the terms
395 | of that license document, provided that the further restriction does
396 | not survive such relicensing or conveying.
397 |
398 | If you add terms to a covered work in accord with this section, you
399 | must place, in the relevant source files, a statement of the
400 | additional terms that apply to those files, or a notice indicating
401 | where to find the applicable terms.
402 |
403 | Additional terms, permissive or non-permissive, may be stated in the
404 | form of a separately written license, or stated as exceptions;
405 | the above requirements apply either way.
406 |
407 | 8. Termination.
408 |
409 | You may not propagate or modify a covered work except as expressly
410 | provided under this License. Any attempt otherwise to propagate or
411 | modify it is void, and will automatically terminate your rights under
412 | this License (including any patent licenses granted under the third
413 | paragraph of section 11).
414 |
415 | However, if you cease all violation of this License, then your
416 | license from a particular copyright holder is reinstated (a)
417 | provisionally, unless and until the copyright holder explicitly and
418 | finally terminates your license, and (b) permanently, if the copyright
419 | holder fails to notify you of the violation by some reasonable means
420 | prior to 60 days after the cessation.
421 |
422 | Moreover, your license from a particular copyright holder is
423 | reinstated permanently if the copyright holder notifies you of the
424 | violation by some reasonable means, this is the first time you have
425 | received notice of violation of this License (for any work) from that
426 | copyright holder, and you cure the violation prior to 30 days after
427 | your receipt of the notice.
428 |
429 | Termination of your rights under this section does not terminate the
430 | licenses of parties who have received copies or rights from you under
431 | this License. If your rights have been terminated and not permanently
432 | reinstated, you do not qualify to receive new licenses for the same
433 | material under section 10.
434 |
435 | 9. Acceptance Not Required for Having Copies.
436 |
437 | You are not required to accept this License in order to receive or
438 | run a copy of the Program. Ancillary propagation of a covered work
439 | occurring solely as a consequence of using peer-to-peer transmission
440 | to receive a copy likewise does not require acceptance. However,
441 | nothing other than this License grants you permission to propagate or
442 | modify any covered work. These actions infringe copyright if you do
443 | not accept this License. Therefore, by modifying or propagating a
444 | covered work, you indicate your acceptance of this License to do so.
445 |
446 | 10. Automatic Licensing of Downstream Recipients.
447 |
448 | Each time you convey a covered work, the recipient automatically
449 | receives a license from the original licensors, to run, modify and
450 | propagate that work, subject to this License. You are not responsible
451 | for enforcing compliance by third parties with this License.
452 |
453 | An "entity transaction" is a transaction transferring control of an
454 | organization, or substantially all assets of one, or subdividing an
455 | organization, or merging organizations. If propagation of a covered
456 | work results from an entity transaction, each party to that
457 | transaction who receives a copy of the work also receives whatever
458 | licenses to the work the party's predecessor in interest had or could
459 | give under the previous paragraph, plus a right to possession of the
460 | Corresponding Source of the work from the predecessor in interest, if
461 | the predecessor has it or can get it with reasonable efforts.
462 |
463 | You may not impose any further restrictions on the exercise of the
464 | rights granted or affirmed under this License. For example, you may
465 | not impose a license fee, royalty, or other charge for exercise of
466 | rights granted under this License, and you may not initiate litigation
467 | (including a cross-claim or counterclaim in a lawsuit) alleging that
468 | any patent claim is infringed by making, using, selling, offering for
469 | sale, or importing the Program or any portion of it.
470 |
471 | 11. Patents.
472 |
473 | A "contributor" is a copyright holder who authorizes use under this
474 | License of the Program or a work on which the Program is based. The
475 | work thus licensed is called the contributor's "contributor version".
476 |
477 | A contributor's "essential patent claims" are all patent claims
478 | owned or controlled by the contributor, whether already acquired or
479 | hereafter acquired, that would be infringed by some manner, permitted
480 | by this License, of making, using, or selling its contributor version,
481 | but do not include claims that would be infringed only as a
482 | consequence of further modification of the contributor version. For
483 | purposes of this definition, "control" includes the right to grant
484 | patent sublicenses in a manner consistent with the requirements of
485 | this License.
486 |
487 | Each contributor grants you a non-exclusive, worldwide, royalty-free
488 | patent license under the contributor's essential patent claims, to
489 | make, use, sell, offer for sale, import and otherwise run, modify and
490 | propagate the contents of its contributor version.
491 |
492 | In the following three paragraphs, a "patent license" is any express
493 | agreement or commitment, however denominated, not to enforce a patent
494 | (such as an express permission to practice a patent or covenant not to
495 | sue for patent infringement). To "grant" such a patent license to a
496 | party means to make such an agreement or commitment not to enforce a
497 | patent against the party.
498 |
499 | If you convey a covered work, knowingly relying on a patent license,
500 | and the Corresponding Source of the work is not available for anyone
501 | to copy, free of charge and under the terms of this License, through a
502 | publicly available network server or other readily accessible means,
503 | then you must either (1) cause the Corresponding Source to be so
504 | available, or (2) arrange to deprive yourself of the benefit of the
505 | patent license for this particular work, or (3) arrange, in a manner
506 | consistent with the requirements of this License, to extend the patent
507 | license to downstream recipients. "Knowingly relying" means you have
508 | actual knowledge that, but for the patent license, your conveying the
509 | covered work in a country, or your recipient's use of the covered work
510 | in a country, would infringe one or more identifiable patents in that
511 | country that you have reason to believe are valid.
512 |
513 | If, pursuant to or in connection with a single transaction or
514 | arrangement, you convey, or propagate by procuring conveyance of, a
515 | covered work, and grant a patent license to some of the parties
516 | receiving the covered work authorizing them to use, propagate, modify
517 | or convey a specific copy of the covered work, then the patent license
518 | you grant is automatically extended to all recipients of the covered
519 | work and works based on it.
520 |
521 | A patent license is "discriminatory" if it does not include within
522 | the scope of its coverage, prohibits the exercise of, or is
523 | conditioned on the non-exercise of one or more of the rights that are
524 | specifically granted under this License. You may not convey a covered
525 | work if you are a party to an arrangement with a third party that is
526 | in the business of distributing software, under which you make payment
527 | to the third party based on the extent of your activity of conveying
528 | the work, and under which the third party grants, to any of the
529 | parties who would receive the covered work from you, a discriminatory
530 | patent license (a) in connection with copies of the covered work
531 | conveyed by you (or copies made from those copies), or (b) primarily
532 | for and in connection with specific products or compilations that
533 | contain the covered work, unless you entered into that arrangement,
534 | or that patent license was granted, prior to 28 March 2007.
535 |
536 | Nothing in this License shall be construed as excluding or limiting
537 | any implied license or other defenses to infringement that may
538 | otherwise be available to you under applicable patent law.
539 |
540 | 12. No Surrender of Others' Freedom.
541 |
542 | If conditions are imposed on you (whether by court order, agreement or
543 | otherwise) that contradict the conditions of this License, they do not
544 | excuse you from the conditions of this License. If you cannot convey a
545 | covered work so as to satisfy simultaneously your obligations under this
546 | License and any other pertinent obligations, then as a consequence you may
547 | not convey it at all. For example, if you agree to terms that obligate you
548 | to collect a royalty for further conveying from those to whom you convey
549 | the Program, the only way you could satisfy both those terms and this
550 | License would be to refrain entirely from conveying the Program.
551 |
552 | 13. Use with the GNU Affero General Public License.
553 |
554 | Notwithstanding any other provision of this License, you have
555 | permission to link or combine any covered work with a work licensed
556 | under version 3 of the GNU Affero General Public License into a single
557 | combined work, and to convey the resulting work. The terms of this
558 | License will continue to apply to the part which is the covered work,
559 | but the special requirements of the GNU Affero General Public License,
560 | section 13, concerning interaction through a network will apply to the
561 | combination as such.
562 |
563 | 14. Revised Versions of this License.
564 |
565 | The Free Software Foundation may publish revised and/or new versions of
566 | the GNU General Public License from time to time. Such new versions will
567 | be similar in spirit to the present version, but may differ in detail to
568 | address new problems or concerns.
569 |
570 | Each version is given a distinguishing version number. If the
571 | Program specifies that a certain numbered version of the GNU General
572 | Public License "or any later version" applies to it, you have the
573 | option of following the terms and conditions either of that numbered
574 | version or of any later version published by the Free Software
575 | Foundation. If the Program does not specify a version number of the
576 | GNU General Public License, you may choose any version ever published
577 | by the Free Software Foundation.
578 |
579 | If the Program specifies that a proxy can decide which future
580 | versions of the GNU General Public License can be used, that proxy's
581 | public statement of acceptance of a version permanently authorizes you
582 | to choose that version for the Program.
583 |
584 | Later license versions may give you additional or different
585 | permissions. However, no additional obligations are imposed on any
586 | author or copyright holder as a result of your choosing to follow a
587 | later version.
588 |
589 | 15. Disclaimer of Warranty.
590 |
591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
599 |
600 | 16. Limitation of Liability.
601 |
602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
610 | SUCH DAMAGES.
611 |
612 | 17. Interpretation of Sections 15 and 16.
613 |
614 | If the disclaimer of warranty and limitation of liability provided
615 | above cannot be given local legal effect according to their terms,
616 | reviewing courts shall apply local law that most closely approximates
617 | an absolute waiver of all civil liability in connection with the
618 | Program, unless a warranty or assumption of liability accompanies a
619 | copy of the Program in return for a fee.
620 |
621 | END OF TERMS AND CONDITIONS
622 |
623 | How to Apply These Terms to Your New Programs
624 |
625 | If you develop a new program, and you want it to be of the greatest
626 | possible use to the public, the best way to achieve this is to make it
627 | free software which everyone can redistribute and change under these terms.
628 |
629 | To do so, attach the following notices to the program. It is safest
630 | to attach them to the start of each source file to most effectively
631 | state the exclusion of warranty; and each file should have at least
632 | the "copyright" line and a pointer to where the full notice is found.
633 |
634 | {one line to give the program's name and a brief idea of what it does.}
635 | Copyright (C) {year} {name of author}
636 |
637 | This program is free software: you can redistribute it and/or modify
638 | it under the terms of the GNU General Public License as published by
639 | the Free Software Foundation, either version 3 of the License, or
640 | (at your option) any later version.
641 |
642 | This program is distributed in the hope that it will be useful,
643 | but WITHOUT ANY WARRANTY; without even the implied warranty of
644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
645 | GNU General Public License for more details.
646 |
647 | You should have received a copy of the GNU General Public License
648 | along with this program. If not, see .
649 |
650 | Also add information on how to contact you by electronic and paper mail.
651 |
652 | If the program does terminal interaction, make it output a short
653 | notice like this when it starts in an interactive mode:
654 |
655 | {project} Copyright (C) {year} {fullname}
656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
657 | This is free software, and you are welcome to redistribute it
658 | under certain conditions; type `show c' for details.
659 |
660 | The hypothetical commands `show w' and `show c' should show the appropriate
661 | parts of the General Public License. Of course, your program's commands
662 | might be different; for a GUI interface, you would use an "about box".
663 |
664 | You should also get your employer (if you work as a programmer) or school,
665 | if any, to sign a "copyright disclaimer" for the program, if necessary.
666 | For more information on this, and how to apply and follow the GNU GPL, see
667 | .
668 |
669 | The GNU General Public License does not permit incorporating your program
670 | into proprietary programs. If your program is a subroutine library, you
671 | may consider it more useful to permit linking proprietary applications with
672 | the library. If this is what you want to do, use the GNU Lesser General
673 | Public License instead of this License. But first, please read
674 | .
675 |
--------------------------------------------------------------------------------
/LLVM/Gen.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.IO;
3 | using System.Linq;
4 | using Cmc.Core;
5 | using Cmc.Decl;
6 | using Cmc.Expr;
7 | using JetBrains.Annotations;
8 |
9 | namespace LLVM
10 | {
11 | public static class Gen
12 | {
13 | [NotNull]
14 | public static string Generate(
15 | [NotNull] string moduleName,
16 | [NotNull] params Declaration[] declarations)
17 | {
18 | var core = new Core();
19 | var analyzedDeclarations = core.Analyze(declarations);
20 | var moduleRef = LLVMSharp.LLVM.ModuleCreateWithName(moduleName);
21 | var builder = LLVMSharp.LLVM.CreateBuilder();
22 | foreach (var analyzedDeclaration in analyzedDeclarations)
23 | GenAstHolder.GenAst(moduleRef, builder, analyzedDeclaration);
24 | // for (var i = 0; i < Constants.StringConstants.Count; i++)
25 | // {
26 | // var str = Constants.StringConstants[i];
27 | // var len = Constants.StringConstantLengths[i];
28 | // }
29 | LLVMSharp.LLVM.DumpModule(moduleRef);
30 | return "";
31 | }
32 |
33 | public static void RunLlvm(
34 | [NotNull] string moduleName,
35 | [NotNull] string outputFile,
36 | [NotNull] params Declaration[] declarations)
37 | {
38 | var generate = Generate(moduleName, declarations);
39 | if (Pragma.PrintDumpInfo) declarations.ToList().ForEach(i => i.PrintDumpInfo());
40 | Console.WriteLine(generate);
41 | File.WriteAllText($"{outputFile}.ll", generate);
42 | CommandLine.RunCommand($"llc-4.0 {outputFile}.ll -filetype=obj");
43 | CommandLine.RunCommand($"gcc {outputFile}.o -o {outputFile}");
44 | }
45 | }
46 | }
--------------------------------------------------------------------------------
/LLVM/GenAst.cs:
--------------------------------------------------------------------------------
1 | using System.Text;
2 | using Cmc;
3 | using Cmc.Core;
4 | using Cmc.Decl;
5 | using Cmc.Expr;
6 | using Cmc.Stmt;
7 | using JetBrains.Annotations;
8 | using LLVMSharp;
9 | using static LLVM.GenDeclaration;
10 | using static LLVM.GenExpression;
11 | using static LLVM.GenStatement;
12 |
13 | namespace LLVM
14 | {
15 | public static class GenAstHolder
16 | {
17 | ///
18 | /// generate llvm ir by the given ast
19 | ///
20 | ///
21 | ///
22 | /// the ast element waiting to be generated
23 | public static void GenAst(
24 | LLVMModuleRef module,
25 | LLVMBuilderRef builder,
26 | [NotNull] Ast element)
27 | {
28 | // convertion
29 | while (element is Statement statement && statement.ConvertedStatementList != null)
30 | element = statement.ConvertedStatementList;
31 | // optimization
32 | while (element.OptimizedStatementList != null && !Pragma.KeepAll)
33 | element = element.OptimizedStatementList;
34 | switch (element)
35 | {
36 | case Expression expression:
37 | GenAstExpression(module, builder, expression);
38 | break;
39 | case Declaration declaration:
40 | GenAstDeclaration(module, builder, declaration);
41 | break;
42 | case Statement statement:
43 | GenAstStatement(module, builder, statement);
44 | break;
45 | }
46 | }
47 | }
48 | }
--------------------------------------------------------------------------------
/LLVM/GenDeclaration.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Linq;
3 | using Cmc;
4 | using Cmc.Decl;
5 | using Cmc.Expr;
6 | using JetBrains.Annotations;
7 | using LLVMSharp;
8 | using static System.StringComparison;
9 | using Type = Cmc.Type;
10 |
11 | namespace LLVM
12 | {
13 | public static class GenDeclaration
14 | {
15 | public static void GenAstDeclaration(
16 | LLVMModuleRef module,
17 | LLVMBuilderRef builder,
18 | [NotNull] Declaration element)
19 | {
20 | switch (element)
21 | {
22 | case TypeDeclaration typeDeclaration:
23 | break;
24 | case VariableDeclaration variable:
25 | if (variable.Type is LambdaType lambdaType)
26 | GenFunction(module, builder, variable, lambdaType);
27 | break;
28 | }
29 | }
30 |
31 | private static void GenFunction(
32 | LLVMModuleRef module,
33 | LLVMBuilderRef builder,
34 | [NotNull] VariableDeclaration variable,
35 | [NotNull] LambdaType lambdaType)
36 | {
37 | var function = LLVMSharp.LLVM.AddFunction(module, variable.Name, GetLlvmType(lambdaType));
38 | LLVMSharp.LLVM.PositionBuilderAtEnd(builder, LLVMSharp.LLVM.AppendBasicBlock(function, "entry"));
39 | GenAstHolder.GenAst(module, builder, (LambdaExpression) variable.Expression);
40 | LLVMSharp.LLVM.VerifyFunction(function, LLVMVerifierFailureAction.LLVMPrintMessageAction);
41 | }
42 |
43 | private static LLVMTypeRef GetLlvmType([NotNull] Type lambdaTypeParam)
44 | {
45 | switch (lambdaTypeParam)
46 | {
47 | case PrimaryType primaryType:
48 | if (string.Equals(primaryType.Name, "f64", Ordinal))
49 | return LLVMSharp.LLVM.DoubleType();
50 | if (string.Equals(primaryType.Name, "f32", Ordinal))
51 | return LLVMTypeRef.FloatType();
52 | if (string.Equals(primaryType.Name, "i8", Ordinal))
53 | return LLVMTypeRef.Int8Type();
54 | if (string.Equals(primaryType.Name, "i16", Ordinal))
55 | return LLVMTypeRef.Int16Type();
56 | if (string.Equals(primaryType.Name, "i32", Ordinal))
57 | return LLVMTypeRef.Int32Type();
58 | if (string.Equals(primaryType.Name, "i64", Ordinal))
59 | return LLVMTypeRef.Int64Type();
60 | break;
61 | case LambdaType lambdaType:
62 | return LLVMTypeRef.FunctionType(GetLlvmType(lambdaType.RetType),
63 | (from type in lambdaType.ParamsList select GetLlvmType(type)).ToArray(), false);
64 | case SecondaryType secondaryType:
65 | break;
66 | }
67 | throw new NotImplementedException();
68 | }
69 | }
70 | }
--------------------------------------------------------------------------------
/LLVM/GenExpression.cs:
--------------------------------------------------------------------------------
1 | using Cmc.Expr;
2 | using JetBrains.Annotations;
3 | using LLVMSharp;
4 |
5 | namespace LLVM
6 | {
7 | public static class GenExpression
8 | {
9 | public static void StoreAtomicExpression(
10 | LLVMBuilderRef builder,
11 | [NotNull] AtomicExpression expression)
12 | {
13 | switch (expression)
14 | {
15 | case IntLiteralExpression integer:
16 | case BoolLiteralExpression boolean:
17 | case VariableExpression variable:
18 | break;
19 | }
20 | }
21 |
22 | public static void GenAstExpression(LLVMModuleRef module, LLVMBuilderRef builder, [NotNull] Expression element)
23 | {
24 | switch (element)
25 | {
26 | case StringLiteralExpression str:
27 | break;
28 | case AtomicExpression expression:
29 | break;
30 | case FunctionCallExpression functionCall:
31 | // function callee and parameters should already be splitted
32 | // into atomic expressions
33 | break;
34 | }
35 | }
36 | }
37 | }
--------------------------------------------------------------------------------
/LLVM/GenStatement.cs:
--------------------------------------------------------------------------------
1 | using Cmc.Decl;
2 | using Cmc.Stmt;
3 | using JetBrains.Annotations;
4 | using LLVMSharp;
5 |
6 | namespace LLVM
7 | {
8 | public static class GenStatement
9 | {
10 | public static void GenAstStatement(
11 | LLVMModuleRef module,
12 | LLVMBuilderRef builder,
13 | [NotNull] Statement element)
14 | {
15 | switch (element)
16 | {
17 | case ReturnStatement returnStatement:
18 | break;
19 | case ExpressionStatement expression:
20 | break;
21 | case StatementList statements:
22 | break;
23 | case Declaration declaration:
24 | break;
25 | }
26 | }
27 | }
28 | }
--------------------------------------------------------------------------------
/LLVM/LLVM.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Debug
5 | AnyCPU
6 | {F0F0AC0C-7DDB-4555-AD24-4724B14DBAB5}
7 | {FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}
8 | Library
9 | Properties
10 | LLVM
11 | LLVM
12 | netcoreapp2.0
13 | 512
14 |
15 |
16 |
17 |
18 |
19 | {17e0de41-2426-4616-8424-ba6c0f97f7c0}
20 | Cmc
21 |
22 |
23 |
24 |
25 |
26 |
27 |
34 |
--------------------------------------------------------------------------------
/LLVMTest/LLVMTest.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 | netcoreapp2.0
4 | false
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 | {17e0de41-2426-4616-8424-ba6c0f97f7c0}
15 | Cmc
16 |
17 |
18 | {f0f0ac0c-7ddb-4555-ad24-4724b14dbab5}
19 | LLVM
20 |
21 |
22 |
--------------------------------------------------------------------------------
/LLVMTest/LlvmCommandLineTests.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using Cmc;
3 | using Cmc.Core;
4 | using Cmc.Decl;
5 | using Cmc.Expr;
6 | using Cmc.Stmt;
7 | using LLVM;
8 | using Microsoft.VisualStudio.TestTools.UnitTesting;
9 |
10 | namespace LLVMTest
11 | {
12 | [TestClass]
13 | public class LlvmCommandLineTests
14 | {
15 | [TestInitialize]
16 | public void Init() => Errors.ErrList.Clear();
17 |
18 | // [TestMethod]
19 | public void CommandLineTest1()
20 | {
21 | Gen.RunLlvm(
22 | "my module",
23 | "out.exe",
24 | new VariableDeclaration(MetaData.Empty,
25 | "i", new IntLiteralExpression(MetaData.Empty, "1", true)),
26 | new VariableDeclaration(MetaData.Empty,
27 | "j", new StringLiteralExpression(MetaData.Empty, "boy next door")),
28 | new VariableDeclaration(MetaData.Empty,
29 | "main", new LambdaExpression(MetaData.Empty,
30 | new StatementList(MetaData.Empty,
31 | new ExpressionStatement(MetaData.Empty,
32 | new FunctionCallExpression(MetaData.Empty,
33 | new VariableExpression(MetaData.Empty, "print"),
34 | new List(new[]
35 | {
36 | new VariableExpression(MetaData.Empty, "j")
37 | }))),
38 | new ReturnStatement(MetaData.Empty,
39 | new IntLiteralExpression(MetaData.Empty, "0", true)))))
40 | );
41 | }
42 | }
43 | }
--------------------------------------------------------------------------------
/LLVMTest/LlvmGenTests.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using Cmc;
4 | using Cmc.Core;
5 | using Cmc.Decl;
6 | using Cmc.Expr;
7 | using Cmc.Stmt;
8 | using LLVM;
9 | using Microsoft.VisualStudio.TestTools.UnitTesting;
10 |
11 | namespace LLVMTest
12 | {
13 | [TestClass]
14 | public class LlvmGenTests
15 | {
16 | ///
17 | /// id function
18 | /// let id = { a: i8 -> a }
19 | ///
20 | private static VariableDeclaration IdDeclaration =>
21 | new VariableDeclaration(MetaData.Empty, "id",
22 | new LambdaExpression(MetaData.Empty,
23 | new StatementList(MetaData.Empty),
24 | new List(new[]
25 | {
26 | new VariableDeclaration(MetaData.Empty, "a", type:
27 | new UnknownType(MetaData.Empty, "i8"))
28 | })));
29 |
30 | // [TestMethod]
31 | public void LlvmGenTest1()
32 | {
33 | var res = Gen.Generate("my module",
34 | new VariableDeclaration(MetaData.Empty,
35 | "i", new IntLiteralExpression(MetaData.Empty, "1", true)),
36 | new VariableDeclaration(MetaData.Empty,
37 | "j", new StringLiteralExpression(MetaData.Empty, "boy next door")),
38 | new VariableDeclaration(MetaData.Empty,
39 | "main", new LambdaExpression(MetaData.Empty,
40 | new StatementList(MetaData.Empty,
41 | new ExpressionStatement(MetaData.Empty,
42 | new FunctionCallExpression(MetaData.Empty,
43 | new VariableExpression(MetaData.Empty, "print"),
44 | new List(new[]
45 | {
46 | new VariableExpression(MetaData.Empty, "j")
47 | }))),
48 | new ReturnStatement(MetaData.Empty,
49 | new IntLiteralExpression(MetaData.Empty, "0", true)))))
50 | );
51 | Console.WriteLine(res);
52 | }
53 |
54 | // [TestMethod]
55 | public void LlvmGenTest2()
56 | {
57 | var body = new StatementList(MetaData.Empty,
58 | new ExpressionStatement(MetaData.Empty,
59 | new FunctionCallExpression(MetaData.Empty,
60 | new VariableExpression(MetaData.Empty, "id"),
61 | new List(new[]
62 | {
63 | new FunctionCallExpression(MetaData.Empty,
64 | new VariableExpression(MetaData.Empty, "id"),
65 | new List(new[]
66 | {
67 | new FunctionCallExpression(MetaData.Empty,
68 | new VariableExpression(MetaData.Empty, "id"),
69 | new List(new[]
70 | {
71 | new IntLiteralExpression(MetaData.Empty, "123", true, 8)
72 | })
73 | )
74 | }))
75 | }))));
76 | var res = Gen.Generate(
77 | "my module",
78 | IdDeclaration,
79 | new VariableDeclaration(MetaData.Empty,
80 | "main", new LambdaExpression(MetaData.Empty, body)));
81 | Console.WriteLine(res);
82 | }
83 |
84 |
85 | // [TestMethod]
86 | public void CodeGenFailTest2() => Assert.ThrowsException(() =>
87 | Gen.RunLlvm(
88 | "my module",
89 | "out.exe",
90 | new VariableDeclaration(MetaData.Empty,
91 | "i", new IntLiteralExpression(MetaData.Empty, "1", true)),
92 | new VariableDeclaration(MetaData.Empty,
93 | "j", new StringLiteralExpression(MetaData.Empty, "boy next door")),
94 | new VariableDeclaration(MetaData.Empty,
95 | "main", new LambdaExpression(MetaData.Empty,
96 | new StatementList(MetaData.Empty,
97 | new ExpressionStatement(MetaData.Empty,
98 | new FunctionCallExpression(MetaData.Empty,
99 | new VariableExpression(MetaData.Empty, "print"),
100 | new List(new[]
101 | {
102 | new VariableExpression(MetaData.Empty, "local")
103 | }))),
104 | new ReturnStatement(MetaData.Empty,
105 | new IntLiteralExpression(MetaData.Empty, "0", true))
106 | ))),
107 | new VariableDeclaration(MetaData.Empty,
108 | "main", new LambdaExpression(MetaData.Empty,
109 | new StatementList(MetaData.Empty,
110 | new ExpressionStatement(MetaData.Empty,
111 | new FunctionCallExpression(MetaData.Empty,
112 | new VariableExpression(MetaData.Empty, "print"),
113 | new List(new[]
114 | {
115 | new VariableExpression(MetaData.Empty, "j")
116 | }))),
117 | new ExpressionStatement(MetaData.Empty,
118 | new FunctionCallExpression(MetaData.Empty,
119 | new VariableExpression(MetaData.Empty, "myfunc"),
120 | new List())),
121 | new ReturnStatement(MetaData.Empty,
122 | new IntLiteralExpression(MetaData.Empty, "0", true)))))
123 | ));
124 |
125 | ///
126 | /// ambiguous main definition
127 | ///
128 | // [TestMethod]
129 | public void CodeGenFailTest1() => Assert.ThrowsException(() =>
130 | Gen.RunLlvm(
131 | "my module",
132 | "out.exe",
133 | new VariableDeclaration(MetaData.Empty,
134 | "i", new IntLiteralExpression(MetaData.Empty, "1", true)),
135 | new VariableDeclaration(MetaData.Empty,
136 | "j", new StringLiteralExpression(MetaData.Empty, "boy next door")),
137 | new VariableDeclaration(MetaData.Empty,
138 | "main", new LambdaExpression(MetaData.Empty,
139 | new StatementList(MetaData.Empty,
140 | new VariableDeclaration(MetaData.Empty,
141 | "local", new StringLiteralExpression(MetaData.Empty, "NullRefTest")),
142 | new ExpressionStatement(MetaData.Empty,
143 | new FunctionCallExpression(MetaData.Empty,
144 | new VariableExpression(MetaData.Empty, "print"),
145 | new List(new[]
146 | {
147 | new VariableExpression(MetaData.Empty, "local")
148 | }))),
149 | new ReturnStatement(MetaData.Empty,
150 | new IntLiteralExpression(MetaData.Empty, "0", true))
151 | ))),
152 | new VariableDeclaration(MetaData.Empty,
153 | "main", new LambdaExpression(MetaData.Empty,
154 | new StatementList(MetaData.Empty,
155 | new VariableDeclaration(MetaData.Empty,
156 | "j", new StringLiteralExpression(MetaData.Empty, "Hello, World")),
157 | new ExpressionStatement(MetaData.Empty,
158 | new FunctionCallExpression(MetaData.Empty,
159 | new VariableExpression(MetaData.Empty, "print"),
160 | new List(new[]
161 | {
162 | new VariableExpression(MetaData.Empty, "j")
163 | }))),
164 | new ExpressionStatement(MetaData.Empty,
165 | new FunctionCallExpression(MetaData.Empty,
166 | new VariableExpression(MetaData.Empty, "myfunc"),
167 | new List())),
168 | new ReturnStatement(MetaData.Empty,
169 | new IntLiteralExpression(MetaData.Empty, "0", true)))))
170 | ));
171 |
172 | [TestInitialize]
173 | public void Init() => Errors.ErrList.Clear();
174 | }
175 | }
--------------------------------------------------------------------------------
/PROGRESS.md:
--------------------------------------------------------------------------------
1 |
2 | Please add feature request here.
3 |
4 | Codes related to some implemented feature can be found by searching their number.
5 |
6 | I.E. Want to find codes related to `lexical scoping` (the 4th one in the `scoping` section)?
7 | [search "FEATURE \#18"](https://github.com/Cm-lang/Cmc/search?utf8=%E2%9C%93&q=%22FEATURE+%2318%22),
8 | and you see the result.
9 |
10 | ## basic language features
11 |
12 | + [X] type aliases (#31)
13 | + [X] no functions (see [README](./README.md))
14 | + [ ] foreign function interfaces
15 |
16 | ## scoping
17 |
18 | + [X] find variables in outer scopes (#3)
19 | + [X] when new variables defined, a new scope starts (#4)
20 | + [X] find all variables matches the name (#5)
21 | + [X] lexical scoping (#18)
22 |
23 | ## types
24 |
25 | + [X] primitive types with name (#0) (tested)
26 | + [X] function type, consisting of the parameters' types and the return type (#6)
27 | + [X] types with multiple type parameter (#7)
28 | + [X] infer function's return type from the return statements (#12) (tested)
29 | + [X] infer variable's type by the assignment expression (#8) (tested)
30 | + [X] resolve types defined in the context (unknown types) (#30) (tested)
31 | + [X] `nulltype` can be assigned to variables with any type (#11) (tested)
32 | + [ ] an HM type system (extra)
33 | + [ ] pointers
34 | + [ ] raw arrays
35 |
36 | ## contracting (extra)
37 |
38 | + [ ] traits or interfaces
39 | + [ ] generic with boundaries
40 |
41 | ## generics
42 |
43 | + [ ] generic lambdas
44 | + [ ] generic structs
45 | + [ ] type inference
46 |
47 | ## variables
48 |
49 | + [X] initialize variable with null or given expression (#13)
50 | + [X] check for mutability (#21)
51 | + [X] check for assignment type (#14) (tested)
52 | + [X] validate lhs (#20)
53 | + [X] check if member exist (#29)
54 | + [ ] inline
55 | + [X] overloading (#33)
56 | + [ ] remove unused local variables but keep the expression (#36)
57 |
58 | ## inline
59 |
60 | + [ ] general inline
61 | + [ ] inline lambda parameters
62 | + [X] inline when directly invoke a lambda expression (#44)
63 | + [X] convert returns into assignment \+ goto (#45)
64 | + [X] keep returning currect when inline (#46)
65 | + [X] keep label return when inline (#47)
66 |
67 | ## functions and lambdas
68 | + [X] check for parameter type (#32)
69 | + [X] the return type should be inferred, or `nulltype` (#19) (tested)
70 | + [X] return statements' types should be same (#24) (tested)
71 | + [ ] return statement exhaustiveness check (extra)
72 | + [X] defaultly no parameter (#22)
73 | + [X] y-combinator-like recur (#37)
74 | + [X] recur can invoke outside lambdas (#38) (tested)
75 |
76 | ## main function
77 |
78 | + [X] only `nulltype` and `i32` returning are allowed (#35)
79 | + [X] check for main function duplication (#40)
80 |
81 | ## structs
82 |
83 | + [X] mutual recursion detection (#34) (tested)
84 | + [ ] generic structs
85 |
86 | ## if
87 |
88 | + [X] check for condition type (#1)
89 | + [X] optional else branch (#2)
90 | + [X] when condition is constant value, delete redundant branch (#17)
91 | + [ ] if as expression (extra)
92 | + [ ] tenary operator
93 |
94 | ## low-level stuffs
95 |
96 | + [X] expression splitting (#42)
97 | + [X] temporary variables are not added to the env (#43)
98 | + [X] string pool (#38)
99 |
100 | ## int
101 |
102 | + [X] only 8, 16, 32, 64 are valid length (#26)
103 | + [X] signed and unsigned (#27)
104 | + [ ] check value is valid
105 |
106 | ## float
107 |
108 | + [X] only 32, 64 are valid length (#41)
109 | + [ ] check value is valid
110 |
111 | ## while
112 |
113 | + [X] check for condition type (#16)
114 | + [ ] do while
115 | + [X] jump statements (#25)
116 |
117 | ## errors
118 |
119 | + [X] when the declared/assignment type are mismatch (#9)
120 | + [X] display line number/file name (#10)
121 |
122 | ## debug
123 |
124 | + [X] display compilation information (#15)
125 | + [X] pretty print strings (#23)
126 |
127 |
128 |
129 | ## LLVM Code Gen
130 |
131 | + [ ] types
132 | + [ ] expressions
133 | + [ ] declarations
134 | + [ ] statements
135 |
136 |
137 |
138 | ### Features given up
139 |
140 | Those features will never be available.
141 |
142 | + types with single type parameter
143 |
144 |
--------------------------------------------------------------------------------
/Parser/ObjectRegex/Node.cs:
--------------------------------------------------------------------------------
1 | #undef DEBUG
2 | using System;
3 | using System.Collections.Generic;
4 | using System.Linq;
5 | using System.Text.RegularExpressions;
6 |
7 | namespace Parser.ObjectRegex
8 | {
9 | public class ObjRegexError : Exception
10 | {
11 | public ObjRegexError(string info) : base(info)
12 | {
13 | }
14 | }
15 |
16 | public class StackOverFlowError : Exception
17 | {
18 | public StackOverFlowError(string info) : base(info)
19 | {
20 | }
21 | }
22 |
23 | public class NameError : Exception
24 | {
25 | public NameError(string info) : base(info)
26 | {
27 | }
28 | }
29 |
30 | public class CompileError : Exception
31 | {
32 | public CompileError(string info) : base(info)
33 | {
34 | }
35 | }
36 |
37 | public static class RegexTool
38 | {
39 | public static (string, Func) ReMatch(string a, bool escape = false)
40 | {
41 | var tokenRule = escape ? Regex.Escape(a) : a;
42 | var re = new Regex(tokenRule);
43 |
44 | return (tokenRule, str =>
45 | {
46 | if (str.Length.Equals(0))
47 | return null;
48 | var r = re.Match(str);
49 | if (r.Index != 0 || r.Length != str.Length)
50 | return null;
51 | return str;
52 | }
53 | );
54 | }
55 | }
56 |
57 | public class Mode : List
58 | {
59 | public string Name;
60 | public string Value;
61 |
62 | public Mode SetName(string name)
63 | {
64 | Name = name;
65 | return this;
66 | }
67 |
68 | public Mode SetValue(string value)
69 | {
70 | Value = value;
71 | return this;
72 | }
73 |
74 | public string Dump(int i = 1)
75 | {
76 | var space = "\n" + new string(' ', 4 * i);
77 | var toDump = string.Join("",
78 | this.Select(
79 | mode => mode.Value != null
80 | ? (mode.Value.Equals("\n")
81 | ? $"{space}{mode.Name}['{@"\n"}']{space}"
82 | : $"{mode.Name}['{mode.Value}']{space}")
83 | : mode.Dump(i + 1)));
84 | return $"{Name}[{toDump}{space}]";
85 | }
86 | }
87 |
88 | public class MetaInfo
89 | {
90 | public int Count = 0;
91 | public int Rdx = 0;
92 |
93 | public int TraceLength = 0;
94 | public int HistoryLength = 0;
95 | public Stack<(int, string)> Trace = null;
96 |
97 |
98 | public Stack<(int, int, int)> History = null;
99 |
100 | protected (int, string) TracePop()
101 | {
102 | --TraceLength;
103 | return Trace.Pop();
104 | }
105 |
106 | public void TracePush((int, string) tp)
107 | {
108 | ++TraceLength;
109 | Trace.Push(tp);
110 | }
111 |
112 | protected (int, int, int) HistoryPop()
113 | {
114 | --HistoryLength;
115 | return History.Pop();
116 | }
117 |
118 | protected void HistoryPush((int, int, int) tp)
119 | {
120 | ++HistoryLength;
121 | History.Push(tp);
122 | }
123 |
124 | public MetaInfo(int count = 0, int rdx = 0, Stack<(int, string)> trace = null)
125 | {
126 | Count = count;
127 | Rdx = rdx;
128 |
129 | if (trace == null)
130 | Trace = new Stack<(int, string)>();
131 | else
132 | foreach (var unused in Trace = trace) ++TraceLength;
133 |
134 | History = new Stack<(int, int, int)>();
135 | }
136 |
137 | public MetaInfo Branch()
138 | {
139 | HistoryPush((Count, Rdx, TraceLength));
140 | return this;
141 | }
142 |
143 | public MetaInfo Rollback()
144 | {
145 | if (HistoryLength == 0)
146 | throw new StackOverFlowError("Pull nothing.");
147 | int traceLength;
148 | (Count, Rdx, traceLength) = HistoryPop();
149 | while (traceLength != TraceLength)
150 | TracePop();
151 | return this;
152 | }
153 |
154 | public MetaInfo Pull()
155 | {
156 | if (HistoryLength == 0)
157 | throw new StackOverFlowError("Pull nothing.");
158 | HistoryPop();
159 | return this;
160 | }
161 |
162 | public string DumpTrace() => string.Join(" ",Trace.Select((a, b) => $"({a}, {b})")
163 | );
164 | }
165 |
166 | public abstract class BaseAst
167 | {
168 | public string Name;
169 | public bool HasRecur;
170 |
171 | public abstract Mode Match(string[] objs, ref MetaInfo meta, bool partial = true);
172 | }
173 |
174 | public class LazyDef : BaseAst
175 | {
176 | public LazyDef(string name) => Name = name;
177 |
178 | public override Mode Match(string[] objs, ref MetaInfo meta, bool partial = true) =>
179 | throw new CompileError($"Ast {Name} Not compiled!");
180 | }
181 |
182 | public class Liter : BaseAst
183 | {
184 | public string TokenRule;
185 |
186 | public Func F;
187 |
188 | public Liter(string i, string name = null, bool escape = false)
189 | {
190 | var (tokenRule, f) = RegexTool.ReMatch(i, escape);
191 | Name = name;
192 | HasRecur = false;
193 | TokenRule = tokenRule;
194 | F = f;
195 | }
196 |
197 | public override Mode Match(string[] objs, ref MetaInfo meta, bool partial = true)
198 | {
199 | var left = objs.Length - meta.Count;
200 | if (left == 0) return null;
201 | var r = F(objs[meta.Count]);
202 | if (r == null || !partial && left != 1) return null;
203 | if (r == "\n") meta.Rdx += 1;
204 | meta.Count += 1;
205 |
206 | return new Mode().SetName(Name).SetValue(r);
207 | }
208 |
209 | public static Liter ELiter(string i, string name = null) =>
210 | new Liter(i, name, true);
211 | }
212 |
213 | public class Ast : BaseAst
214 | {
215 | public List> Possibilities;
216 | protected BaseAst[][] Cache;
217 | public Dictionary CompileClosure;
218 | public bool Compiled;
219 |
220 | public Ast()
221 | {
222 | }
223 |
224 | public Ast(ref Dictionary compileClosure,
225 | string name = null,
226 | params BaseAst[][] ebnf)
227 | {
228 | Name = name ?? throw new NameError("Name not found! Each kind of ast should have a identity name.");
229 |
230 | Cache = ebnf;
231 | if (compileClosure.Keys.Contains(name))
232 | throw new NameError("Name of Ast should be identified!");
233 | compileClosure[name] = this;
234 | Compiled = false;
235 | CompileClosure = compileClosure;
236 | Possibilities = new List>();
237 | }
238 |
239 | public Ast Compile(ref HashSet recurSearcher)
240 | {
241 | if (recurSearcher == null)
242 | recurSearcher = new HashSet {Name};
243 | else
244 | {
245 | if (recurSearcher.Contains(Name))
246 | {
247 | HasRecur = true;
248 | Compiled = true;
249 | }
250 | else
251 | recurSearcher.Add(Name);
252 | }
253 |
254 | if (Compiled)
255 | return this;
256 |
257 | foreach (var es in Cache)
258 | {
259 | var possibility = new List();
260 | Possibilities.Add(possibility);
261 | foreach (var e in es)
262 | {
263 | switch (e)
264 | {
265 | case Liter liter:
266 | possibility.Add(liter);
267 | break;
268 | case LazyDef _:
269 | {
270 | var refered = CompileClosure[e.Name];
271 | refered.Compile(ref recurSearcher);
272 | possibility.Add(refered);
273 | if (refered.HasRecur)
274 | HasRecur = true;
275 | break;
276 | }
277 | case Seq refered:
278 | {
279 | refered.Compile(ref recurSearcher);
280 | possibility.Add(refered);
281 | if (refered.HasRecur)
282 | HasRecur = true;
283 | break;
284 | }
285 | default:
286 | throw new CompileError("Unsolved Ast Type");
287 | }
288 | }
289 | }
290 | #if DEBUG
291 | if (has_recur)
292 | Console.WriteLine("Found Recursive EBNF Node => "+name);
293 | #endif
294 |
295 | Cache = null;
296 | Compiled = true;
297 | return this;
298 | }
299 |
300 | public override Mode Match(string[] objs, ref MetaInfo meta, bool partial = true)
301 | {
302 | if (meta == null)
303 | {
304 | throw new ArgumentNullException(nameof(meta));
305 | }
306 | #if DEBUG
307 | Console.WriteLine($"{this.name} ");
308 | #endif
309 |
310 | var res = new Mode().SetName(Name);
311 | var hasRes = false;
312 |
313 | foreach (var possibility in Possibilities)
314 | {
315 | meta.Branch(); // Make a branch in case of resetting.
316 | foreach (var thing in possibility)
317 | {
318 | var history = (meta.Count, thing.Name);
319 | Mode r;
320 |
321 | if (thing.HasRecur)
322 | {
323 | if (meta.Trace.Contains(history))
324 | {
325 | Console.WriteLine("Found Left Recursion. Dealed.");
326 | r = null;
327 | }
328 | else
329 | {
330 | meta.Trace.Push(history);
331 | r = thing.Match(objs, ref meta, true);
332 | }
333 | }
334 | else
335 | {
336 | // DEBUG View: the trace of parsing.
337 | #if DEBUG
338 | meta.trace.Push(history);
339 | #endif
340 | r = thing.Match(objs, ref meta, true);
341 | }
342 |
343 | if (r == null)
344 | {
345 | // Do not match current possibility, then rollback.
346 | res.Clear();
347 | meta.Rollback();
348 | goto ContinueForNewPossibility;
349 | }
350 |
351 | if (thing is Seq)
352 | {
353 | res.AddRange(r);
354 | }
355 | else
356 | {
357 | res.Add(r);
358 | }
359 |
360 | #if DEBUG
361 | Console.WriteLine($"{thing.name} <= {r.Dump()}");
362 | #endif
363 | }
364 | #if DEBUG
365 | Console.WriteLine($"RETURN from {this.name} ");
366 | #endif
367 | hasRes = true;
368 | break;
369 | ContinueForNewPossibility:
370 | ;
371 | }
372 | if (!hasRes)
373 | {
374 | #if DEBUG
375 | Console.WriteLine($"RETURN None from {this.name} ");
376 | #endif
377 | return null;
378 | }
379 | meta.Pull();
380 | var left = objs.Length - meta.Count;
381 | if (partial || left == 0)
382 | {
383 | #if DEBUG
384 | Console.WriteLine($"RETURN Some from {this.name}");
385 | #endif
386 | return res;
387 | }
388 | #if DEBUG
389 | Console.WriteLine($"RETURN None from {this.name} (No partial and do not match all)");
390 | #endif
391 | return null;
392 | }
393 | }
394 |
395 | public class Seq : Ast
396 | {
397 | int _atleast;
398 | int _atmost;
399 |
400 | public Seq()
401 | {
402 | }
403 |
404 | public Seq(ref Dictionary compileClosure,
405 | string name = null,
406 | int atleast = 1,
407 | int atmost = -1,
408 | params BaseAst[][] ebnf) : base(ref compileClosure, name, ebnf)
409 | {
410 | _atleast = atleast;
411 | _atmost = atmost;
412 | }
413 |
414 | public override Mode Match(string[] objs, ref MetaInfo meta, bool partial = true)
415 | {
416 | if (meta == null)
417 | throw new ArgumentNullException(nameof(meta));
418 | var res = new Mode().SetName(Name);
419 | var left = objs.Length - meta.Count;
420 | if (left == 0)
421 | return _atleast == 0 ? res : null;
422 | meta.Branch();
423 | Mode r;
424 | int idx = 0;
425 | if (_atmost > 0)
426 | while (true)
427 | {
428 | if (idx >= _atmost) break;
429 | r = base.Match(objs, ref meta, true);
430 | if (r == null)
431 | break;
432 | res.AddRange(r);
433 | ++idx;
434 | }
435 | else
436 | while (true)
437 | {
438 | r = base.Match(objs, ref meta, true);
439 | if (r == null) break;
440 | res.AddRange(r);
441 | ++idx;
442 | }
443 | #if DEBUG
444 | Console.WriteLine($"{name} <= {res.Dump()}");
445 | #endif
446 | if (idx < _atleast)
447 | {
448 | meta.Rollback();
449 | return null;
450 | }
451 | meta.Pull();
452 | return res;
453 | }
454 | }
455 | }
--------------------------------------------------------------------------------
/Parser/Parser.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 | Library
4 | netcoreapp2.0
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Parser/ReadMe.md:
--------------------------------------------------------------------------------
1 | # EBNFParser for CSharp
2 |
3 |
4 | ### EBNF SelfExamination
5 |
6 | [EBNF Grammar](../selfexamine.ebnf)
7 |
8 | ```bnf
9 | Expr ::= Or+
10 | Or ::= AtomExpr ('|' AtomExpr)*
11 |
12 | AtomExpr::= Atom Trailer
13 | Atom ::= Name | Str | '[' Expr ']' | '(' Expr ')'
14 |
15 |
16 | Def ::= '::='
17 | Equals ::= Name Def Expr
18 |
19 | Trailer::= ['*' | '+' | '{' Number{1 2} '}']
20 |
21 | Name ::= R'[a-zA-Z_][a-zA-Z0-9]*'
22 | Str ::= R'[\w|\W]*'
23 | Number ::= R'\d+'
24 | ```
25 |
26 | \-----------------------------
27 | [Token for EBNF](./LanguageTest/SelfExaminationForEBNF/Token.cs)
28 | \-----------------------------
29 |
30 | [Parser for EBNF](./LanguageTest/SelfExaminationForEBNF/Parser.cs)
31 |
32 | Here is the definition of `Atom`.
33 |
34 | ```c#
35 | var atom = new Ast(
36 | ref compileClosure,
37 | "Atom", new BaseAst[]
38 | {
39 | DefualtToken.Name
40 | }, new BaseAst[]
41 | {
42 | DefualtToken.Str
43 | }, new BaseAst[]
44 | {
45 | DefualtToken.Lb,
46 | new LazyDef("Expr"),
47 | DefualtToken.Rb
48 | }, new BaseAst[]
49 | {
50 | DefualtToken.Lp,
51 | new LazyDef("Expr"),
52 | DefualtToken.Rp
53 | });
54 | ```
55 |
56 | \-----------------------------
57 | ### Core Codes
58 |
59 | [Node.cs](./ObjectRegex/Node.cs)
60 | The results parsed are organized by `CSharp.ObjectRegex.Mode`.
61 | If you want to dump the results, just use `CSharp.ObjectRegex.Mode.Dump()`.
62 | Here is an example at [testres_csharp](../testres_csharp)
63 |
--------------------------------------------------------------------------------
/ParserTest/BootStrap.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.IO;
3 | using System.Linq;
4 | using Microsoft.VisualStudio.TestTools.UnitTesting;
5 | using Parser.ObjectRegex;
6 | using ParserTest.Bootstrap;
7 |
8 | namespace ParserTest
9 | {
10 | [TestClass]
11 | public class NaiveTest
12 | {
13 | [TestMethod]
14 | public void TestBootstrap()
15 | {
16 | const string code = @"
17 | Expr ::= Or+
18 | Or ::= AtomExpr ('|' AtomExpr)*
19 |
20 | AtomExpr ::= Atom Trailer
21 | Atom ::= Name | Str | '[' Expr ']' | '(' Expr ')'
22 |
23 | Def ::= '::='
24 | Equals ::= Name Def Expr
25 |
26 | Trailer ::= ['*' | '+' | '{' Number{1 2} '}']
27 |
28 | Name ::= R'[a-zA-Z_][a-zA-Z0-9]*'
29 | Str ::= R'[\w|\W]*'
30 | Number ::= R'\d+'
31 | ";
32 | // Initialize parser
33 | var parser = BootstrapParser.GenParser();
34 | var re = DefualtToken.Token();
35 |
36 | // Gen tokenized words
37 | var tokens = re.Matches(code).Select(i => i.ToString()).ToArray();
38 |
39 | // Parsing
40 | var meta = new MetaInfo();
41 | Console.WriteLine(parser.Stmt.Match(
42 | tokens,
43 | ref meta,
44 | false
45 | ).Dump());
46 | }
47 | }
48 | }
--------------------------------------------------------------------------------
/ParserTest/Bootstrap/BootstrapParser.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using Parser.ObjectRegex;
3 |
4 | namespace ParserTest.Bootstrap
5 | {
6 | public class BootstrapParser
7 | {
8 | public Ast Expr;
9 | public Ast Or;
10 | public Ast Split;
11 | public Ast Atom;
12 | public Ast AtomExpr;
13 | public Ast Eq;
14 | public Ast Stmt;
15 | public Ast Trailer;
16 | public Dictionary CompileClosure;
17 |
18 | public BootstrapParser(Ast expr, Ast or, Ast split,
19 | Ast atom, Ast atomExpr,
20 | Ast eq, Ast stmt,
21 | Ast trailer,
22 | Dictionary compileClosure
23 | )
24 | {
25 | Expr = expr;
26 | Or = or;
27 | Split = split;
28 | Atom = atom;
29 | Eq = eq;
30 | AtomExpr = atomExpr;
31 | Stmt = stmt;
32 | Trailer = trailer;
33 | CompileClosure = compileClosure;
34 | }
35 |
36 | public static BootstrapParser GenParser()
37 | {
38 | var compileClosure = new Dictionary();
39 | var expr = new Ast(
40 | compileClosure: ref compileClosure,
41 | name: "Expr",
42 | ebnf: new BaseAst[]
43 | {
44 | new Seq(
45 | compileClosure: ref compileClosure,
46 | atleast: 0,
47 | name: "Or+",
48 | ebnf: new BaseAst[]
49 | {
50 | new LazyDef("Or")
51 | }
52 | )
53 | }
54 | );
55 |
56 | var or = new Ast(
57 | compileClosure: ref compileClosure,
58 | name: "Or",
59 | ebnf: new BaseAst[]
60 | {
61 | new LazyDef("AtomExpr"),
62 | new Seq(
63 | compileClosure: ref compileClosure,
64 | atleast: 0,
65 | name: "('|' AtomExpr)*",
66 | ebnf: new BaseAst[]
67 | {
68 | DefualtToken.OrSign,
69 | new LazyDef("AtomExpr")
70 | }
71 | )
72 | }
73 | );
74 | var atomExpr = new Ast(
75 | compileClosure: ref compileClosure,
76 | name: "AtomExpr",
77 | ebnf: new BaseAst[]
78 | {
79 | new LazyDef("Atom"),
80 | new LazyDef("Trailer")
81 | }
82 | );
83 | var trailer = new Seq(
84 | ref compileClosure,
85 | "Trailer",
86 | 0,
87 | ebnf: new[]
88 | {
89 | new BaseAst[]
90 | {
91 | DefualtToken.SeqStar
92 | },
93 | new BaseAst[]
94 | {
95 | DefualtToken.SeqPlus
96 | },
97 | new BaseAst[]
98 | {
99 | DefualtToken.Lbb,
100 | new Seq(
101 | compileClosure: ref compileClosure,
102 | atleast: 1,
103 | atmost: 2,
104 | name: "Number{1 2}",
105 | ebnf: new BaseAst[]
106 | {
107 | DefualtToken.Number
108 | }),
109 | DefualtToken.Rbb
110 | }
111 | }
112 | );
113 | var atom = new Ast(
114 | ref compileClosure,
115 | "Atom", new BaseAst[]
116 | {
117 | DefualtToken.Name
118 | }, new BaseAst[]
119 | {
120 | DefualtToken.Str
121 | }, new BaseAst[]
122 | {
123 | DefualtToken.Lb,
124 | new LazyDef("Expr"),
125 | DefualtToken.Rb
126 | }, new BaseAst[]
127 | {
128 | DefualtToken.Lp,
129 | new LazyDef("Expr"),
130 | DefualtToken.Rp
131 | });
132 | var eq = new Ast(
133 | compileClosure: ref compileClosure,
134 | name: "Eq",
135 | ebnf: new BaseAst[]
136 | {
137 | DefualtToken.Name,
138 | DefualtToken.Def,
139 | new LazyDef("Expr")
140 | }
141 | );
142 | var stmt = new Ast(
143 | compileClosure: ref compileClosure,
144 | name: "Stmt",
145 | ebnf: new BaseAst[]
146 | {
147 | new Seq(
148 | compileClosure: ref compileClosure,
149 | name: "Eq*",
150 | atleast: 0,
151 | ebnf: new BaseAst[]
152 | {
153 | new LazyDef("SPLIT"),
154 | new LazyDef("Eq"),
155 | new LazyDef("SPLIT")
156 | }
157 | )
158 | }
159 | );
160 | var split = new Seq(
161 | compileClosure: ref compileClosure,
162 | name: "SPLIT",
163 | atleast: 0,
164 | ebnf: new BaseAst[]
165 | {
166 | DefualtToken.Newline
167 | }
168 | );
169 | var namestore = new HashSet();
170 | split.Compile(ref namestore);
171 | stmt.Compile(ref namestore);
172 |
173 | return new BootstrapParser(
174 | expr: expr,
175 | atom: atom,
176 | eq: eq,
177 | or: or,
178 | stmt: stmt,
179 | atomExpr: atomExpr,
180 | trailer: trailer,
181 | split: split,
182 | compileClosure: compileClosure
183 | );
184 | }
185 | }
186 | }
--------------------------------------------------------------------------------
/ParserTest/Bootstrap/Token.cs:
--------------------------------------------------------------------------------
1 | using System.Text.RegularExpressions;
2 | using Parser.ObjectRegex;
3 |
4 | namespace ParserTest.Bootstrap
5 | {
6 | public static class DefualtToken
7 | {
8 | public static readonly Liter Name = new Liter("[a-zA-Z_][a-zA-Z0-9]*", "Name");
9 | public static readonly Liter Number = new Liter(@"\d+", "Number");
10 | public static readonly Liter Str = new Liter(@"[R]{0,1}'[\w|\W]*?'", "Str");
11 | public static readonly Liter Newline = new Liter("\n", "NEWLINE");
12 |
13 | public static readonly Liter Lbb = Liter.ELiter("{", "LBB");
14 | public static readonly Liter Lb = Liter.ELiter("[", "LB");
15 | public static readonly Liter Lp = Liter.ELiter("(", "LP");
16 |
17 | public static readonly Liter Rbb = Liter.ELiter("}", "RBB");
18 | public static readonly Liter Rb = Liter.ELiter("]", "RB");
19 | public static readonly Liter Rp = Liter.ELiter(")", "RP");
20 |
21 | public static readonly Liter SeqStar = Liter.ELiter("*", "SeqStar");
22 | public static readonly Liter SeqPlus = Liter.ELiter("+", "SeqPlus");
23 | public static readonly Liter Def = Liter.ELiter("::=", "Def");
24 | public static readonly Liter OrSign = Liter.ELiter("|", "OrSign");
25 |
26 | public static Regex Token() => new Regex(
27 | string.Join("|",
28 | Name.TokenRule,
29 | Number.TokenRule,
30 | Str.TokenRule,
31 | Newline.TokenRule,
32 | Lbb.TokenRule,
33 | Rbb.TokenRule,
34 | Lp.TokenRule,
35 | Rp.TokenRule,
36 | Lb.TokenRule,
37 | Rb.TokenRule,
38 | SeqPlus.TokenRule,
39 | SeqStar.TokenRule,
40 | Def.TokenRule,
41 | OrSign.TokenRule)
42 | );
43 | }
44 | }
--------------------------------------------------------------------------------
/ParserTest/ParserTest.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 | netcoreapp2.0
4 | false
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Cmc
2 |
3 | 
4 |
5 |
9 |
10 | [](https://travis-ci.org/Cm-lang/Cmc)
11 | [](https://github.com/Cm-lang/Cmc)
12 | [](https://github.com/Cm-lang/Cm-Document/issues/new)
13 | [](http://llvm.org/)
14 | [](https://www.nuget.org/packages/Cmc/)
15 | [](https://www.nuget.org/packages/Cmc/)
16 | [](https://github.com/Cm-lang/Cmc)
17 |
18 | The compiler for the Cm programming language.
19 |
20 | Cm is a statically typed native language (targeting LLVM and GLSL),
21 | with first-class lambdas and powerful type inference.
22 |
23 | There are no `function`s in this language, it treats them as lambda variables.
24 | With compile-time inlining, functional style codes can run as fast as imperative style ones.
25 |
26 | This language doesn't have subtyping,
27 | it organize data with structs and extension methods.
28 |
29 | Maybe there will be an optional H-M type system, and hole-oriented programming (like Idris).
30 |
31 | ## Document
32 |
33 | + [AST Design](./Cmc/Cm_AST_Design.yml)
34 | + [Progress](./PROGRESS.md)
35 |
36 | ## Used Project
37 |
38 | + [LLVMSharp](https://github.com/Microsoft/LLVMSharp)
39 |
--------------------------------------------------------------------------------