Skip to content

Commit 91d2bc4

Browse files
Merge pull request #9 from Cm-lang/master
Merge*10
2 parents 08cc5e5 + f739b24 commit 91d2bc4

35 files changed

+1009
-776
lines changed

Cmc.sln

Lines changed: 47 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,73 @@
1-
2-
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio 15
4-
VisualStudioVersion = 15.0.26730.12
5-
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Cmc", "Cmc\Cmc.csproj", "{17E0DE41-2426-4616-8424-BA6C0F97F7C0}"
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}"
76
EndProject
8-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LLVM", "LLVM\LLVM.csproj", "{F0F0AC0C-7DDB-4555-AD24-4724B14DBAB5}"
7+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CmcTest", "CmcTest\CmcTest.csproj", "{81B2DCB5-26FE-40BF-B942-DB192E2B7F9C}"
98
EndProject
10-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CmcExec", "CmcExec\CmcExec.csproj", "{7172A461-5A1E-4297-B3F8-F4FC4192113D}"
9+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LLVM", "LLVM\LLVM.csproj", "{4FA21EBF-A178-4E12-A610-DF9FBCC161A1}"
1110
EndProject
12-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CmcTest", "CmcTest\CmcTest.csproj", "{707EF8CB-4B0D-46E6-B5D1-B3AF98EB7007}"
11+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LLVMTest", "LLVMTest\LLVMTest.csproj", "{A1B637D0-CA61-4671-A6AC-ACB7CF937F5F}"
1312
EndProject
14-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Parser", "Parser\Parser.csproj", "{5F1B0D93-D606-4F92-85C0-A8EA01853439}"
13+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CmcExec", "CmcExec\CmcExec.csproj", "{FDDDB91D-1AAB-477A-B3E3-0B374FB85808}"
1514
EndProject
16-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LLVMTest", "LLVMTest\LLVMTest.csproj", "{09FCC9CC-8540-48BD-A76D-798C135BA275}"
15+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Parser", "Parser\Parser.csproj", "{E95D4571-3CE3-48EF-8107-AEEB654E3A77}"
1716
EndProject
18-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ParserTest", "ParserTest\ParserTest.csproj", "{B6F5D94A-8FAE-470C-8977-60D4A900073D}"
17+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ParserTest", "ParserTest\ParserTest.csproj", "{C2AFB02B-420B-4915-B094-0FC4D8CB2648}"
1918
EndProject
2019
Global
2120
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2221
Debug|Any CPU = Debug|Any CPU
2322
Release|Any CPU = Release|Any CPU
2423
EndGlobalSection
2524
GlobalSection(ProjectConfigurationPlatforms) = postSolution
26-
{17E0DE41-2426-4616-8424-BA6C0F97F7C0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27-
{17E0DE41-2426-4616-8424-BA6C0F97F7C0}.Debug|Any CPU.Build.0 = Debug|Any CPU
28-
{17E0DE41-2426-4616-8424-BA6C0F97F7C0}.Release|Any CPU.ActiveCfg = Release|Any CPU
29-
{17E0DE41-2426-4616-8424-BA6C0F97F7C0}.Release|Any CPU.Build.0 = Release|Any CPU
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
3029
{F0F0AC0C-7DDB-4555-AD24-4724B14DBAB5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
3130
{F0F0AC0C-7DDB-4555-AD24-4724B14DBAB5}.Debug|Any CPU.Build.0 = Debug|Any CPU
3231
{F0F0AC0C-7DDB-4555-AD24-4724B14DBAB5}.Release|Any CPU.ActiveCfg = Release|Any CPU
3332
{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
3437
{7172A461-5A1E-4297-B3F8-F4FC4192113D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
3538
{7172A461-5A1E-4297-B3F8-F4FC4192113D}.Debug|Any CPU.Build.0 = Debug|Any CPU
3639
{7172A461-5A1E-4297-B3F8-F4FC4192113D}.Release|Any CPU.ActiveCfg = Release|Any CPU
3740
{7172A461-5A1E-4297-B3F8-F4FC4192113D}.Release|Any CPU.Build.0 = Release|Any CPU
38-
{707EF8CB-4B0D-46E6-B5D1-B3AF98EB7007}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
39-
{707EF8CB-4B0D-46E6-B5D1-B3AF98EB7007}.Debug|Any CPU.Build.0 = Debug|Any CPU
40-
{707EF8CB-4B0D-46E6-B5D1-B3AF98EB7007}.Release|Any CPU.ActiveCfg = Release|Any CPU
41-
{707EF8CB-4B0D-46E6-B5D1-B3AF98EB7007}.Release|Any CPU.Build.0 = Release|Any CPU
42-
{5F1B0D93-D606-4F92-85C0-A8EA01853439}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
43-
{5F1B0D93-D606-4F92-85C0-A8EA01853439}.Debug|Any CPU.Build.0 = Debug|Any CPU
44-
{5F1B0D93-D606-4F92-85C0-A8EA01853439}.Release|Any CPU.ActiveCfg = Release|Any CPU
45-
{5F1B0D93-D606-4F92-85C0-A8EA01853439}.Release|Any CPU.Build.0 = Release|Any CPU
46-
{09FCC9CC-8540-48BD-A76D-798C135BA275}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
47-
{09FCC9CC-8540-48BD-A76D-798C135BA275}.Debug|Any CPU.Build.0 = Debug|Any CPU
48-
{09FCC9CC-8540-48BD-A76D-798C135BA275}.Release|Any CPU.ActiveCfg = Release|Any CPU
49-
{09FCC9CC-8540-48BD-A76D-798C135BA275}.Release|Any CPU.Build.0 = Release|Any CPU
50-
{B6F5D94A-8FAE-470C-8977-60D4A900073D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
51-
{B6F5D94A-8FAE-470C-8977-60D4A900073D}.Debug|Any CPU.Build.0 = Debug|Any CPU
52-
{B6F5D94A-8FAE-470C-8977-60D4A900073D}.Release|Any CPU.ActiveCfg = Release|Any CPU
53-
{B6F5D94A-8FAE-470C-8977-60D4A900073D}.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
5469
EndGlobalSection
5570
GlobalSection(SolutionProperties) = preSolution
5671
HideSolutionNode = FALSE
5772
EndGlobalSection
58-
GlobalSection(ExtensibilityGlobals) = postSolution
59-
SolutionGuid = {AD49DC85-384E-4670-924C-64214DC200F2}
60-
EndGlobalSection
6173
EndGlobal

Cmc/Cm_AST_Design.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Ast:
1111
VariableDeclaration
1212
StructDeclaration
1313
TypeDeclaration
14+
ReturnLabelDeclaration
15+
JumpLabelDeclaration
1416
Expression:
1517
FunctionCallExpression
1618
MemberAccessExpression

Cmc/Cmc.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<Description>The compiler for the Cm programming language</Description>
2222
<PackageLicenseUrl>https://www.gnu.org/licenses/gpl-3.0.en.html</PackageLicenseUrl>
2323
<PackageProjectUrl>https://github.com/Cm-lang/Cmc</PackageProjectUrl>
24-
<Company>Cmc-language</Company>
24+
<Company>Cm-lang</Company>
2525
<Authors>ice1000</Authors>
2626
<PackageReleaseNotes>First release.</PackageReleaseNotes>
2727
<PackageId>Cmc</PackageId>

Cmc/Core/Ast.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public abstract class Ast
1515
/// <summary>
1616
/// inline/constant folding/etc.
1717
/// </summary>
18-
[CanBeNull] public Statement OptimizedStatementList = null;
18+
[CanBeNull] public StatementList OptimizedStatementList = null;
1919

2020
protected Ast(MetaData metaData) => MetaData = metaData;
2121

Cmc/Core/Environment.cs

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,34 @@
55
using Cmc.Expr;
66
using Cmc.Stmt;
77
using JetBrains.Annotations;
8+
using static System.StringComparison;
89

910
namespace Cmc.Core
1011
{
1112
public class Environment
1213
{
13-
public static readonly Environment Galaxy;
14-
public static readonly Environment SolarSystem;
14+
/// <summary>
15+
/// preload functions/types
16+
/// </summary>
17+
public static Environment Galaxy;
18+
19+
private static readonly MetaData BuiltIn = new MetaData(-1, "[built-in]");
20+
21+
public static Environment SolarSystem;
1522

1623
[NotNull] public readonly IList<Declaration> Declarations = new List<Declaration>();
1724

1825
// FEATURE #18
1926
[CanBeNull] private readonly Environment _outer;
2027

21-
static Environment()
28+
public static void Initialize()
2229
{
2330
Galaxy = new Environment();
2431
SolarSystem = new Environment(Galaxy);
2532

2633
// FEATURE #0
27-
foreach (var typeDeclaration in from builtinType in new[]
34+
foreach (var typeDeclaration in
35+
from builtinType in new[]
2836
{
2937
"i8", "i16", "i32", "i64",
3038
"u8", "u16", "u32", "u64",
@@ -34,23 +42,23 @@ static Environment()
3442
PrimaryType.BoolType
3543
}
3644
select new TypeDeclaration(
37-
MetaData.BuiltIn,
45+
BuiltIn,
3846
builtinType,
39-
new PrimaryType(MetaData.BuiltIn, builtinType)))
47+
new PrimaryType(BuiltIn, builtinType)))
4048
Galaxy.Declarations.Add(typeDeclaration);
41-
var puts = new VariableDeclaration(MetaData.BuiltIn, "print",
42-
new BuiltinLambda(MetaData.BuiltIn,
43-
new StatementList(MetaData.BuiltIn),
44-
new PrimaryType(MetaData.BuiltIn, PrimaryType.NullType),
45-
new List<VariableDeclaration>(new[]
49+
var puts = new ExternDeclaration(BuiltIn, "print", null,
50+
new LambdaType(BuiltIn,
51+
new List<Type>
4652
{
47-
new VariableDeclaration(MetaData.BuiltIn, "s", type:
48-
new PrimaryType(MetaData.BuiltIn, PrimaryType.StringType))
49-
})));
53+
new PrimaryType(BuiltIn, PrimaryType.StringType)
54+
},
55+
new PrimaryType(BuiltIn, PrimaryType.NullType)));
5056
puts.SurroundWith(Galaxy);
5157
SolarSystem.Declarations.Add(puts);
5258
}
5359

60+
static Environment() => Initialize();
61+
5462
public Environment(Environment outer = null) => _outer = outer;
5563

5664
/// FEATURE #3
@@ -63,7 +71,7 @@ public IList<Declaration> FindAllDeclarationsByName([NotNull] string name)
6371
{
6472
list.AddRange(
6573
from declaration in env.Declarations
66-
where string.Equals(declaration.Name, name, StringComparison.Ordinal)
74+
where string.Equals(declaration.Name, name, Ordinal)
6775
select declaration);
6876
} while ((env = env._outer) != null);
6977
return list;
@@ -76,19 +84,26 @@ where string.Equals(declaration.Name, name, StringComparison.Ordinal)
7684
/// <param name="name">the name of the required declaration</param>
7785
/// <returns>the declaration</returns>
7886
[CanBeNull]
79-
public Declaration FindDeclarationByName([NotNull] string name)
80-
{
81-
var env = this;
82-
do
83-
{
84-
foreach (var declaration in
85-
from declaration in env.Declarations
86-
where string.Equals(declaration.Name, name, StringComparison.Ordinal)
87-
select declaration)
88-
return declaration;
89-
} while ((env = env._outer) != null);
90-
return null;
91-
}
87+
public Declaration FindDeclarationByName([NotNull] string name) =>
88+
FindDeclarationSatisfies(i => string.Equals(i.Name, name, Ordinal));
89+
90+
/// <summary>
91+
/// If there's no such declaration, this funciton will return null.
92+
/// </summary>
93+
/// <param name="name">
94+
/// the name of the required declaration.
95+
/// if it's null, find the nearest one.
96+
/// </param>
97+
/// <returns>the declaration</returns>
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));
92107

93108
/// <summary>
94109
/// Find a declaration satisfying one constraint

Cmc/Core/MetaData.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,5 @@ public string GetErrorHeader() =>
2323

2424
private static int _count;
2525
public static readonly MetaData Empty = new MetaData(_count++, "Unknown");
26-
public static readonly MetaData BuiltIn = new MetaData(-1, "[built-in]");
2726
}
2827
}

Cmc/Decl/Declaration.cs

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using System.Linq;
23
using Cmc.Core;
34
using Cmc.Stmt;
45
using JetBrains.Annotations;
@@ -10,17 +11,17 @@ namespace Cmc.Decl
1011
{
1112
public class Declaration : Statement
1213
{
13-
public readonly Modifier Modifier;
14+
public readonly Modifier[] Modifiers;
1415
[NotNull] public readonly string Name;
15-
public bool Used = false;
16+
public ulong UsageCount;
1617

1718
public Declaration(
1819
MetaData metaData,
1920
[NotNull] string name,
20-
Modifier modifier) : base(metaData)
21+
Modifier[] modifiers = null) : base(metaData)
2122
{
2223
Name = name;
23-
Modifier = modifier;
24+
Modifiers = modifiers;
2425
}
2526
}
2627

@@ -36,8 +37,8 @@ public TypeDeclaration(
3637
MetaData metaData,
3738
[NotNull] string name,
3839
[NotNull] Type type,
39-
Modifier modifier = Modifier.Private)
40-
: base(metaData, name, modifier)
40+
Modifier[] modifiers = null)
41+
: base(metaData, name, modifiers ?? new[] {Modifier.Private})
4142
{
4243
Type = type;
4344
}
@@ -52,8 +53,8 @@ public StructDeclaration(
5253
MetaData metaData,
5354
[NotNull] string name,
5455
[NotNull] IList<VariableDeclaration> fieldList,
55-
Modifier modifier = Modifier.Private) :
56-
base(metaData, name, modifier)
56+
Modifier[] modifiers = null) :
57+
base(metaData, name, modifiers ?? new[] {Modifier.Private})
5758
{
5859
FieldList = fieldList;
5960
Type = new SecondaryType(metaData, name, this);
@@ -68,6 +69,28 @@ public override void SurroundWith(Environment environment)
6869
}
6970
}
7071

72+
public class ExternDeclaration : Declaration
73+
{
74+
public Type Type;
75+
public readonly bool Mutability;
76+
77+
public ExternDeclaration(
78+
MetaData metaData,
79+
[NotNull] string name,
80+
Modifier[] modifiers,
81+
Type type,
82+
bool mutability = false) :
83+
base(metaData, name, modifiers ?? new[] {Modifier.Private})
84+
{
85+
Type = type;
86+
Mutability = mutability;
87+
}
88+
89+
public override IEnumerable<string> Dump() => new[]
90+
{$"extern declaration [{Name}]:\n"}
91+
.Concat(Type.Dump());
92+
}
93+
7194
/// <summary>
7295
/// Probably useless
7396
/// </summary>
@@ -79,13 +102,15 @@ public Macro(
79102
MetaData metaData,
80103
[NotNull] string name,
81104
[NotNull] string content,
82-
Modifier modifier = Modifier.Private) :
83-
base(metaData, name, modifier)
105+
Modifier[]modifiers = null) :
106+
base(metaData, name, modifiers ?? new[] {Modifier.Private})
84107
{
85108
Content = content;
86109
}
87110

88-
public override IEnumerable<string> Dump() =>
89-
new[] {"macro(this shouldn't appear)\n"};
111+
public override IEnumerable<string> Dump() => new[]
112+
{
113+
"macro(this shouldn't appear)\n"
114+
};
90115
}
91116
}

0 commit comments

Comments
 (0)