├── Nova.Antlr
├── App.config
├── Program.cs
├── AntlrTool.cs
├── Properties
│ └── AssemblyInfo.cs
├── Nova.Antlr.csproj
└── bin
│ └── Debug
│ ├── NovaLexer.g4
│ └── NovaParser.g4
├── Nova.Compiler
├── App.config
├── Properties
│ └── AssemblyInfo.cs
├── Program.cs
└── Nova.Compiler.csproj
├── Nova.Bytecode
├── Enums
│ ├── MethodType.cs
│ ├── ModifiersEnum.cs
│ ├── ContainerType.cs
│ ├── OperatorsEnum.cs
│ └── NativesEnum.cs
├── IO
│ ├── IByteElement.cs
│ ├── MainPointEntry.cs
│ ├── ByteField.cs
│ ├── Symbol.cs
│ ├── ByteClass.cs
│ ├── ByteMethod.cs
│ └── NovFile.cs
├── Codes
│ ├── ICode.cs
│ ├── DuplicateCode.cs
│ ├── AddCode.cs
│ ├── DivCode.cs
│ ├── MulCode.cs
│ ├── SubCode.cs
│ ├── ReturnCode.cs
│ ├── VectCreateCode.cs
│ ├── LoadClassCode.cs
│ ├── PushNullCode.cs
│ ├── StructPushCurrent.cs
│ ├── PushIntCode.cs
│ ├── LoadCode.cs
│ ├── JumpCode.cs
│ ├── StoreCode.cs
│ ├── StructCreateCode.cs
│ ├── JumpIfFalseCode.cs
│ ├── PushConstCode.cs
│ ├── NativeCallCode.cs
│ ├── StructCallMethodCode.cs
│ ├── ComparaisonCode.cs
│ ├── StructLoadMemberCode.cs
│ ├── StructStoreMemberCode.cs
│ ├── CtorCallCode.cs
│ ├── MethodCallCode.cs
│ ├── StoreGlobalCode.cs
│ └── LoadGlobalCode.cs
├── Symbols
│ ├── SymbolType.cs
│ ├── Symbol.cs
│ └── SymbolTable.cs
├── Properties
│ └── AssemblyInfo.cs
├── Generation
│ └── ByteBlock.cs
└── Nova.ByteCode.csproj
├── Nova
├── Expressions
│ ├── Accessors
│ │ ├── AccessorType.cs
│ │ ├── IAccessorExpression.cs
│ │ └── Accessor.cs
│ ├── EmptyExpression.cs
│ ├── ConstIntExpression.cs
│ ├── ConstStringExpression.cs
│ ├── ConstBoolExpression.cs
│ ├── Expression.cs
│ ├── VariableNameExpression.cs
│ ├── NativeCallExpression.cs
│ ├── MethodCallExpression.cs
│ ├── ExpressionNode.cs
│ ├── StructCallCtorStatement.cs
│ └── OperatorExpression.cs
├── Members
│ ├── Types.cs
│ ├── IAccessible.cs
│ ├── IChild.cs
│ ├── ISemanticMember.cs
│ ├── Interface.cs
│ ├── Variable.cs
│ ├── Field.cs
│ ├── Class.cs
│ └── Method.cs
├── IO
│ ├── IByteData.cs
│ ├── Using.cs
│ ├── NvFile.cs
│ ├── ClassesContainer.cs
│ └── NovBuilder.cs
├── packages.config
├── CompilationState.cs
├── Parser
│ ├── ParserUtils.cs
│ ├── Errors
│ │ └── NovaParsingErrorHandler.cs
│ └── Listeners
│ │ ├── ClassListener.cs
│ │ ├── ClassMemberListener.cs
│ │ ├── StatementListener.cs
│ │ └── ExpressionListener.cs
├── Constants.cs
├── Types
│ ├── TypeManager.cs
│ └── NovaType.cs
├── Semantics
│ ├── SemanticalError.cs
│ └── SemanticsValidator.cs
├── Statements
│ ├── EmptyStatement.cs
│ ├── ReturnStatement.cs
│ ├── Statement.cs
│ ├── DeclarationStatement.cs
│ ├── ExpressionStatement.cs
│ ├── AssignationStatement.cs
│ ├── VectorCreationStatement.cs
│ ├── WhileStatement.cs
│ ├── ForStatement.cs
│ └── IfStatement.cs
├── Antlr
│ ├── NovaLexer.tokens
│ └── NovaParser.tokens
├── Properties
│ └── AssemblyInfo.cs
└── Nova.csproj
├── Standard Library
├── nova.nv
└── nova2.nv
├── Nova.Utils
├── Constants.cs
├── IO
│ ├── CppBinaryWriter.cs
│ └── CppBinaryReader.cs
├── Properties
│ └── AssemblyInfo.cs
├── Logger.cs
└── Nova.Utils.csproj
├── Readme.md
└── Nova.sln
/Nova.Antlr/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Nova.Compiler/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/Nova.Bytecode/Enums/MethodType.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace Nova.Bytecode.Enums
8 | {
9 | public enum MethodType
10 | {
11 | Method,
12 | Ctor,
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/Nova/Expressions/Accessors/AccessorType.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace Nova.Expressions.Accessors
8 | {
9 | public enum AccessorType
10 | {
11 | Field,
12 | Method,
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/Nova.Bytecode/Enums/ModifiersEnum.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace Nova.ByteCode.Enums
8 | {
9 | public enum ModifiersEnum
10 | {
11 | @public = 0x1,
12 | @private = 0x2,
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/Nova/Members/Types.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace Nova.Members
8 | {
9 | public enum Types
10 | {
11 | Unit,
12 | String,
13 | Int,
14 | Bool,
15 | Struct,
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/Nova.Bytecode/Enums/ContainerType.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace Nova.Bytecode.Enums
8 | {
9 | public enum ContainerType
10 | {
11 | @class = 0,
12 | @struct,
13 | primitive,
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/Nova/IO/IByteData.cs:
--------------------------------------------------------------------------------
1 | using Nova.ByteCode.IO;
2 | using System;
3 | using System.Collections.Generic;
4 | using System.Linq;
5 | using System.Text;
6 | using System.Threading.Tasks;
7 |
8 | namespace Nova.IO
9 | {
10 | public interface IByteData
11 | {
12 | IByteElement GetByteElement(ClassesContainer container, IByteElement parent);
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/Nova/Members/IAccessible.cs:
--------------------------------------------------------------------------------
1 | using Nova.Semantics;
2 | using System;
3 | using System.Collections.Generic;
4 | using System.Linq;
5 | using System.Text;
6 | using System.Threading.Tasks;
7 |
8 | namespace Nova.Members
9 | {
10 | public interface IAccessible
11 | {
12 | public Class GetContextualClass(SemanticsValidator validator);
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/Nova/packages.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Nova.Bytecode/IO/IByteElement.cs:
--------------------------------------------------------------------------------
1 | using Nova.Utils.IO;
2 | using System;
3 | using System.Collections.Generic;
4 | using System.IO;
5 | using System.Linq;
6 | using System.Text;
7 | using System.Threading.Tasks;
8 |
9 | namespace Nova.ByteCode.IO
10 | {
11 | public interface IByteElement
12 | {
13 | void Serialize(CppBinaryWriter writer);
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/Nova/CompilationState.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace Nova
8 | {
9 | public enum CompilationState
10 | {
11 | Parsing,
12 | TypeLink,
13 | SemanticalValidation,
14 | BytecodeGeneration,
15 | End,
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/Nova/Members/IChild.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace Nova.Members
8 | {
9 | public interface IChild
10 | {
11 | Class ParentClass
12 | {
13 | get;
14 | }
15 | IChild Parent
16 | {
17 | get;
18 | }
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/Nova.Bytecode/Enums/OperatorsEnum.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace Nova.ByteCode.Enums
8 | {
9 | public enum OperatorsEnum
10 | {
11 | Plus,
12 | Minus,
13 | Multiply,
14 | Divide,
15 | Equals,
16 | Different,
17 | Inferior,
18 | Superior,
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/Nova/Parser/ParserUtils.cs:
--------------------------------------------------------------------------------
1 | using Nova.ByteCode.Enums;
2 | using System;
3 | using System.Collections.Generic;
4 | using System.Linq;
5 | using System.Text;
6 | using System.Threading.Tasks;
7 |
8 | namespace Nova.Parser
9 | {
10 | class ParserUtils
11 | {
12 | public static ModifiersEnum ParseModifier(string modifier)
13 | {
14 | return (ModifiersEnum)Enum.Parse(typeof(ModifiersEnum), modifier);
15 | }
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/Nova.Bytecode/Codes/ICode.cs:
--------------------------------------------------------------------------------
1 | using Nova.Utils.IO;
2 | using System;
3 | using System.Collections.Generic;
4 | using System.IO;
5 | using System.Linq;
6 | using System.Text;
7 | using System.Threading.Tasks;
8 |
9 | namespace Nova.ByteCode.Codes
10 | {
11 | public interface ICode
12 | {
13 | int OpId
14 | {
15 | get;
16 | }
17 |
18 | void Serialize(CppBinaryWriter writer);
19 |
20 | int GetSize();
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/Nova.Bytecode/Enums/NativesEnum.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace Nova.Bytecode.Enums
8 | {
9 | public enum NativesEnum
10 | {
11 | Unknown = 0,
12 | Print = 1,
13 | Readl = 2,
14 | GetVectSize = 3,
15 | AddVect = 4,
16 | AtVect = 5,
17 | SetVect = 6,
18 | StrGet = 7,
19 | NewLine = 8,
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/Nova/Members/ISemanticMember.cs:
--------------------------------------------------------------------------------
1 | using Nova.ByteCode.Generation;
2 | using Nova.IO;
3 | using Nova.Semantics;
4 | using System;
5 | using System.Collections.Generic;
6 | using System.Linq;
7 | using System.Text;
8 | using System.Threading.Tasks;
9 |
10 | namespace Nova.Members
11 | {
12 | public interface ISemanticMember
13 | {
14 | void GenerateBytecode(ClassesContainer container, ByteBlock context);
15 |
16 | void ValidateSemantics(SemanticsValidator validator);
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/Nova.Bytecode/Symbols/SymbolType.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace Nova.Bytecode.Symbols
8 | {
9 | public enum SymbolType
10 | {
11 | Unknown = 0,
12 | Local, // human (where human is a local variable)
13 | ClassMember, // human where human is a class field.
14 | StructMember, // human where human is a struct field
15 | ExternalMember, // Class.Field (public static)
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/Nova.Bytecode/Codes/DuplicateCode.cs:
--------------------------------------------------------------------------------
1 | using Nova.ByteCode.Codes;
2 | using Nova.Utils.IO;
3 | using System;
4 | using System.Collections.Generic;
5 | using System.Linq;
6 | using System.Text;
7 | using System.Threading.Tasks;
8 |
9 | namespace Nova.Bytecode.Codes
10 | {
11 | public class DuplicateCode : ICode
12 | {
13 | public int OpId => 29;
14 |
15 | public int GetSize()
16 | {
17 | return 0;
18 | }
19 |
20 | public void Serialize(CppBinaryWriter writer)
21 | {
22 |
23 | }
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/Standard Library/nova.nv:
--------------------------------------------------------------------------------
1 |
2 | primitive unit
3 | {
4 |
5 | }
6 |
7 | primitive char
8 | {
9 |
10 | }
11 | primitive string
12 | {
13 | public char get(string self,int index)
14 | {
15 | return ~StrGet(self,index)
16 | }
17 | }
18 | primitive int
19 | {
20 | public int MaxValue = 2147483647
21 | public int MinValue = -2147483647
22 |
23 | public int pow2(int self)
24 | {
25 | return self * self
26 | }
27 | }
28 |
29 | class Nova
30 | {
31 | public unit Print(string value)
32 | {
33 | ~Print(value)
34 | }
35 | public unit PrintLine(string value)
36 | {
37 | ~Print(value)
38 | ~NewLine()
39 | }
40 |
41 | }
42 |
43 |
--------------------------------------------------------------------------------
/Nova.Bytecode/Codes/AddCode.cs:
--------------------------------------------------------------------------------
1 | using Nova.ByteCode.Codes;
2 | using Nova.Utils.IO;
3 | using System;
4 | using System.Collections.Generic;
5 | using System.Linq;
6 | using System.Text;
7 | using System.Threading.Tasks;
8 |
9 | namespace Nova.Bytecode.Codes
10 | {
11 | public class AddCode : ICode
12 | {
13 | public int OpId => 1;
14 |
15 | public int GetSize()
16 | {
17 | return 0;
18 | }
19 |
20 | public void Serialize(CppBinaryWriter writer)
21 | {
22 |
23 | }
24 | public override string ToString()
25 | {
26 | return "(" + OpId + ") " + "Add";
27 | }
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/Nova.Bytecode/Codes/DivCode.cs:
--------------------------------------------------------------------------------
1 | using Nova.ByteCode.Codes;
2 | using Nova.Utils.IO;
3 | using System;
4 | using System.Collections.Generic;
5 | using System.Linq;
6 | using System.Text;
7 | using System.Threading.Tasks;
8 |
9 | namespace Nova.Bytecode.Codes
10 | {
11 | public class DivCode : ICode
12 | {
13 | public int OpId => 4;
14 |
15 | public int GetSize()
16 | {
17 | return 0;
18 | }
19 |
20 | public void Serialize(CppBinaryWriter writer)
21 | {
22 |
23 | }
24 | public override string ToString()
25 | {
26 | return "(" + OpId + ") " + "Div";
27 | }
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/Nova.Bytecode/Codes/MulCode.cs:
--------------------------------------------------------------------------------
1 | using Nova.ByteCode.Codes;
2 | using Nova.Utils.IO;
3 | using System;
4 | using System.Collections.Generic;
5 | using System.Linq;
6 | using System.Text;
7 | using System.Threading.Tasks;
8 |
9 | namespace Nova.Bytecode.Codes
10 | {
11 | public class MulCode : ICode
12 | {
13 | public int OpId => 12;
14 |
15 | public int GetSize()
16 | {
17 | return 0;
18 | }
19 |
20 | public void Serialize(CppBinaryWriter writer)
21 | {
22 |
23 | }
24 | public override string ToString()
25 | {
26 | return "(" + OpId + ") " + "Mul";
27 | }
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/Nova.Bytecode/Codes/SubCode.cs:
--------------------------------------------------------------------------------
1 | using Nova.ByteCode.Codes;
2 | using Nova.Utils.IO;
3 | using System;
4 | using System.Collections.Generic;
5 | using System.Linq;
6 | using System.Text;
7 | using System.Threading.Tasks;
8 |
9 | namespace Nova.Bytecode.Codes
10 | {
11 | public class SubCode : ICode
12 | {
13 | public int OpId => 27;
14 |
15 | public int GetSize()
16 | {
17 | return 0;
18 | }
19 |
20 | public void Serialize(CppBinaryWriter writer)
21 | {
22 |
23 | }
24 | public override string ToString()
25 | {
26 | return "(" + OpId + ") " + "Sub";
27 | }
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/Nova/IO/Using.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace Nova.IO
8 | {
9 | public enum UsingType
10 | {
11 | Ref,
12 | Std,
13 | }
14 | public class Using
15 | {
16 | public UsingType Type
17 | {
18 | get;
19 | set;
20 | }
21 | public string Value
22 | {
23 | get;
24 | set;
25 | }
26 | public Using(UsingType type,string value)
27 | {
28 | this.Type = type;
29 | this.Value = value;
30 | }
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/Nova/Constants.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Reflection;
5 | using System.Text;
6 | using System.Threading.Tasks;
7 |
8 | namespace Nova
9 | {
10 | public class Constants
11 | {
12 | public const string BytecodeFileExtension = ".nov";
13 |
14 | public const string NovaSourceFileExtension = ".nv";
15 |
16 | public const string DefaultOuputPath = "output.nov";
17 |
18 | public const string NovFileHeader = "NEXEC";
19 |
20 | public const string MainPointEntryMethodName = "main";
21 |
22 | public const string STDVectorClassName = "Vector"; // weird stuff here
23 |
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/Nova.Bytecode/Codes/ReturnCode.cs:
--------------------------------------------------------------------------------
1 | using Nova.Utils.IO;
2 | using System;
3 | using System.Collections.Generic;
4 | using System.IO;
5 | using System.Linq;
6 | using System.Text;
7 | using System.Threading.Tasks;
8 |
9 | namespace Nova.ByteCode.Codes
10 | {
11 | public class ReturnCode : ICode
12 | {
13 | public int OpId => 18;
14 |
15 | public ReturnCode()
16 | {
17 |
18 | }
19 | public void Serialize(CppBinaryWriter writer)
20 | {
21 |
22 | }
23 | public int GetSize()
24 | {
25 | return 0;
26 | }
27 | public override string ToString()
28 | {
29 | return "(" + OpId + ") " + "Return";
30 | }
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/Nova.Bytecode/Codes/VectCreateCode.cs:
--------------------------------------------------------------------------------
1 | using Nova.ByteCode.Codes;
2 | using Nova.Utils.IO;
3 | using System;
4 | using System.Collections.Generic;
5 | using System.Linq;
6 | using System.Text;
7 | using System.Threading.Tasks;
8 |
9 | namespace Nova.Bytecode.Codes
10 | {
11 | public class VectCreateCode : ICode
12 | {
13 | public int OpId => 28;
14 |
15 | private int size;
16 |
17 | public VectCreateCode(int size)
18 | {
19 | this.size = size;
20 | }
21 |
22 | public int GetSize()
23 | {
24 | return 1;
25 | }
26 |
27 | public void Serialize(CppBinaryWriter writer)
28 | {
29 | writer.Write(size);
30 | }
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/Nova.Bytecode/Codes/LoadClassCode.cs:
--------------------------------------------------------------------------------
1 | using Nova.ByteCode.Codes;
2 | using Nova.Utils.IO;
3 | using System;
4 | using System.Collections.Generic;
5 | using System.Linq;
6 | using System.Text;
7 | using System.Threading.Tasks;
8 |
9 | namespace Nova.Bytecode.Codes
10 | {
11 | public class LoadClassCode : ICode
12 | {
13 | public int OpId => 29;
14 |
15 | private int classId;
16 |
17 | public LoadClassCode(int classId)
18 | {
19 | this.classId = classId;
20 | }
21 |
22 | public int GetSize()
23 | {
24 | return 1;
25 | }
26 |
27 | public void Serialize(CppBinaryWriter writer)
28 | {
29 | writer.Write(classId);
30 | }
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/Nova.Bytecode/Codes/PushNullCode.cs:
--------------------------------------------------------------------------------
1 | using Nova.ByteCode.Codes;
2 | using Nova.Utils.IO;
3 | using System;
4 | using System.Collections.Generic;
5 | using System.Linq;
6 | using System.Text;
7 | using System.Threading.Tasks;
8 |
9 | namespace Nova.Bytecode.Codes
10 | {
11 | public class PushNullCode : ICode
12 | {
13 | public int OpId => 16;
14 |
15 | public PushNullCode()
16 | {
17 |
18 | }
19 |
20 | public override string ToString()
21 | {
22 | return "(" + OpId + ") " + "PushNull";
23 | }
24 |
25 | public void Serialize(CppBinaryWriter writer)
26 | {
27 |
28 | }
29 | public int GetSize()
30 | {
31 | return 0;
32 | }
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/Nova/Members/Interface.cs:
--------------------------------------------------------------------------------
1 | using Nova.ByteCode.Enums;
2 | using Nova.IO;
3 | using Nova.Semantics;
4 | using Nova.Statements;
5 | using System;
6 | using System.Collections.Generic;
7 | using System.IO;
8 | using System.Linq;
9 | using System.Text;
10 | using System.Threading.Tasks;
11 |
12 | namespace Nova.Members
13 | {
14 | public class Interface
15 | {
16 | public const string INTERFACE_PATTERN = @"^\s*interface\s+([a-zA-Z_$][a-zA-Z_$0-9]*)$";
17 |
18 | public Class ParentClass => throw new NotImplementedException();
19 |
20 | public string Name => throw new NotImplementedException();
21 |
22 | public ModifiersEnum Modifiers => throw new NotImplementedException();
23 |
24 | public IChild Parent => throw new NotImplementedException();
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/Nova/Expressions/EmptyExpression.cs:
--------------------------------------------------------------------------------
1 | using Antlr4.Runtime;
2 | using Nova.ByteCode.Generation;
3 | using Nova.IO;
4 | using Nova.Members;
5 | using Nova.Semantics;
6 | using System;
7 | using System.Collections.Generic;
8 | using System.Linq;
9 | using System.Text;
10 | using System.Threading.Tasks;
11 |
12 | namespace Nova.Expressions
13 | {
14 | public class EmptyExpression : Expression
15 | {
16 | public EmptyExpression(IChild parent, ParserRuleContext context) : base(parent, context)
17 | {
18 |
19 | }
20 | public override void GenerateBytecode(ClassesContainer container, ByteBlock context)
21 | {
22 |
23 | }
24 |
25 | public override void ValidateSemantics(SemanticsValidator validator)
26 | {
27 |
28 | }
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/Nova.Bytecode/Codes/StructPushCurrent.cs:
--------------------------------------------------------------------------------
1 | using Nova.ByteCode.Codes;
2 | using Nova.Utils.IO;
3 | using System;
4 | using System.Collections.Generic;
5 | using System.Linq;
6 | using System.Text;
7 | using System.Threading.Tasks;
8 |
9 | namespace Nova.Bytecode.Codes
10 | {
11 | /*
12 | * human.Name = "value"
13 | */
14 | public class StructPushCurrent : ICode
15 | {
16 | public int OpId => 25;
17 |
18 | public StructPushCurrent()
19 | {
20 |
21 | }
22 |
23 | public void Serialize(CppBinaryWriter writer)
24 | {
25 | }
26 | public override string ToString()
27 | {
28 | return "(" + OpId + ") " + "StructPushCurrent";
29 | }
30 | public int GetSize()
31 | {
32 | return 0;
33 | }
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/Nova.Utils/Constants.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Reflection;
5 | using System.Text;
6 | using System.Threading.Tasks;
7 |
8 | namespace Nova.Utils
9 | {
10 | public class Constants
11 | {
12 | public const string INTERMEDIATE_LANGUAGE_FILE_EXTENSION = ".nov";
13 |
14 | public const string SOURCE_CODE_FILE_EXTENSION = ".nv";
15 |
16 | public const string DEFAULT_OUTPUT_PATH = "output.nov";
17 |
18 | public const string NOV_FILE_HEADER = "NEXEC";
19 |
20 | public const char BRACKET_START_DELIMITER = '{';
21 |
22 | public const char BRACKET_END_DELIMITER = '}';
23 |
24 | public const string MAIN_METHOD_NAME = "Main";
25 |
26 | public static Assembly ASSEMBLY = Assembly.GetExecutingAssembly();
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/Nova/Parser/Errors/NovaParsingErrorHandler.cs:
--------------------------------------------------------------------------------
1 | using Antlr4.Runtime;
2 | using Antlr4.Runtime.Misc;
3 | using Nova.Utils;
4 | using System;
5 | using System.Collections.Generic;
6 | using System.Linq;
7 | using System.Text;
8 | using System.Threading.Tasks;
9 |
10 | namespace Nova.Parser.Errors
11 | {
12 | public class NovaParsingErrorHandler : IAntlrErrorListener
13 | {
14 | public int ErrorsCount
15 | {
16 | get;
17 | set;
18 | }
19 | public void SyntaxError([NotNull] IRecognizer recognizer, [Nullable] IToken offendingSymbol, int line, int charPositionInLine, [NotNull] string msg, [Nullable] RecognitionException e)
20 | {
21 | Logger.Write(line + ":" + charPositionInLine + " " + msg, LogType.SyntaxicError);
22 | ErrorsCount++ ;
23 | }
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/Nova.Bytecode/Codes/PushIntCode.cs:
--------------------------------------------------------------------------------
1 | using Nova.Utils.IO;
2 | using System;
3 | using System.Collections.Generic;
4 | using System.IO;
5 | using System.Linq;
6 | using System.Text;
7 | using System.Threading.Tasks;
8 |
9 | namespace Nova.ByteCode.Codes
10 | {
11 | public class PushIntCode : ICode
12 | {
13 | public int OpId => 15;
14 |
15 | private int value;
16 |
17 | public PushIntCode(int value)
18 | {
19 | this.value = value;
20 | }
21 | public override string ToString()
22 | {
23 | return "(" + OpId + ") " + "PushInt " + value;
24 | }
25 |
26 | public void Serialize(CppBinaryWriter writer)
27 | {
28 | writer.Write(value);
29 | }
30 | public int GetSize()
31 | {
32 | return 1;
33 | }
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/Nova/Expressions/Accessors/IAccessorExpression.cs:
--------------------------------------------------------------------------------
1 | using Antlr4.Runtime;
2 | using Nova.Members;
3 | using System;
4 | using System.Collections.Generic;
5 | using System.Linq;
6 | using System.Text;
7 | using System.Threading.Tasks;
8 |
9 | namespace Nova.Expressions.Accessors
10 | {
11 | public abstract class AccessorExpression : Expression
12 | {
13 | public AccessorExpression ParentAccessor
14 | {
15 | get;
16 | set;
17 | }
18 | public string Name
19 | {
20 | get;
21 | set;
22 | }
23 | public abstract AccessorType AccessorType
24 | {
25 | get;
26 | }
27 | public AccessorExpression(IChild parent, ParserRuleContext context) : base(parent, context)
28 | {
29 |
30 | }
31 |
32 |
33 |
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/Nova.Bytecode/Symbols/Symbol.cs:
--------------------------------------------------------------------------------
1 | using Nova.ByteCode.IO;
2 | using Nova.Utils;
3 | using Nova.Utils.IO;
4 | using System;
5 | using System.Collections.Generic;
6 | using System.Linq;
7 | using System.Text;
8 | using System.Threading.Tasks;
9 |
10 | namespace Nova.Bytecode.Symbols
11 | {
12 | public class Symbol : IByteElement
13 | {
14 | public int Id;
15 | public string Type; // useless ?
16 |
17 | public Symbol(int id,string type)
18 | {
19 | this.Id = id;
20 | this.Type = type;
21 | }
22 |
23 |
24 | public void Serialize(CppBinaryWriter writer)
25 | {
26 | writer.Write(Id);
27 | writer.Write(Type);
28 | }
29 | public override string ToString()
30 | {
31 | return "{" + Id + ": " + Type + "}";
32 | }
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/Standard Library/nova2.nv:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | struct ListElement
5 | {
6 | public ListElement Next
7 | public string Value
8 |
9 | }
10 | struct List
11 | {
12 | public ListElement Head
13 | public int Size
14 |
15 | public ListElement Current
16 |
17 | -> List()
18 | {
19 | Head = -> ListElement()
20 | Current = Head
21 | Size = 0
22 | }
23 | public void Add(string element)
24 | {
25 | if (Size == 0)
26 | {
27 | Current.Value = element
28 | }
29 | else
30 | {
31 | Current.Next = -> ListElement()
32 | Current.Next.Value = element
33 | Current = Current.Next
34 | }
35 |
36 | Size = Size + 1
37 | }
38 | public void Print()
39 | {
40 | ListElement current = Head
41 |
42 | int i = 0
43 |
44 | while (i < Size)
45 | {
46 | Nova.Print(current.Value)
47 | current = current.Next
48 | i = i + 1
49 | }
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/Nova/Types/TypeManager.cs:
--------------------------------------------------------------------------------
1 | using Nova.IO;
2 | using Nova.Members;
3 | using System;
4 | using System.Collections.Generic;
5 | using System.Linq;
6 | using System.Text;
7 | using System.Threading.Tasks;
8 |
9 | namespace Nova.Types
10 | {
11 | public class TypeManager
12 | {
13 | private Dictionary Types = new Dictionary();
14 |
15 | public TypeManager()
16 | {
17 | }
18 |
19 |
20 | public NovaType GetTypeInstance(string type)
21 | {
22 | NovaType result = null;
23 | Types.TryGetValue(type, out result);
24 | return result;
25 | }
26 |
27 | public void Register(Class @class, bool primitive)
28 | {
29 | NovaType type = new NovaType(@class, primitive);
30 | Types.Add(type.Name, type);
31 | }
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/Nova.Bytecode/Codes/LoadCode.cs:
--------------------------------------------------------------------------------
1 | using Nova.Utils.IO;
2 | using System;
3 | using System.Collections.Generic;
4 | using System.IO;
5 | using System.Linq;
6 | using System.Text;
7 | using System.Threading.Tasks;
8 |
9 | namespace Nova.ByteCode.Codes
10 | {
11 | public class LoadCode : ICode
12 | {
13 | public int OpId => 7;
14 |
15 | private int variableId;
16 |
17 | public LoadCode(int variableId)
18 | {
19 | this.variableId = variableId;
20 | }
21 |
22 | public override string ToString()
23 | {
24 | return "(" + OpId + ") " + "Load " + variableId;
25 | }
26 |
27 | public void Serialize(CppBinaryWriter writer)
28 | {
29 | writer.Write(variableId);
30 | }
31 | public int GetSize()
32 | {
33 | return 1;
34 | }
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/Nova.Bytecode/Codes/JumpCode.cs:
--------------------------------------------------------------------------------
1 | using Nova.Utils.IO;
2 | using System;
3 | using System.Collections.Generic;
4 | using System.IO;
5 | using System.Linq;
6 | using System.Text;
7 | using System.Threading.Tasks;
8 |
9 | namespace Nova.ByteCode.Codes
10 | {
11 | public class JumpCode : ICode
12 | {
13 | public int OpId => 5;
14 |
15 | public int targetIndex;
16 |
17 | public JumpCode(int targetIndex)
18 | {
19 | this.targetIndex = targetIndex;
20 | }
21 |
22 | public void Serialize(CppBinaryWriter writer)
23 | {
24 | writer.Write(targetIndex);
25 | }
26 |
27 | public override string ToString()
28 | {
29 | return "(" + OpId + ") " + "Jump " + targetIndex;
30 | }
31 | public int GetSize()
32 | {
33 | return 1;
34 | }
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/Nova.Bytecode/Codes/StoreCode.cs:
--------------------------------------------------------------------------------
1 | using Nova.Utils.IO;
2 | using System;
3 | using System.Collections.Generic;
4 | using System.IO;
5 | using System.Linq;
6 | using System.Text;
7 | using System.Threading.Tasks;
8 |
9 | namespace Nova.ByteCode.Codes
10 | {
11 | public class StoreCode : ICode
12 | {
13 | public int OpId => 19;
14 |
15 | private int variableId;
16 |
17 | public StoreCode(int variableId)
18 | {
19 | this.variableId = variableId;
20 | }
21 |
22 | public void Serialize(CppBinaryWriter writer)
23 | {
24 | writer.Write(variableId);
25 | }
26 |
27 | public override string ToString()
28 | {
29 | return "(" + OpId + ") " + "Store " + variableId;
30 | }
31 | public int GetSize()
32 | {
33 | return 1;
34 | }
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/Nova.Bytecode/Codes/StructCreateCode.cs:
--------------------------------------------------------------------------------
1 | using Nova.ByteCode.Codes;
2 | using Nova.Utils.IO;
3 | using System;
4 | using System.Collections.Generic;
5 | using System.Linq;
6 | using System.Text;
7 | using System.Threading.Tasks;
8 |
9 | namespace Nova.Bytecode.Codes
10 | {
11 | public class StructCreateCode : ICode
12 | {
13 | public int OpId => 23;
14 |
15 | private int classId;
16 |
17 | public StructCreateCode(int classId)
18 | {
19 | this.classId = classId;
20 | }
21 | public override string ToString()
22 | {
23 | return "(" + OpId + ") " + "StructCreate " + classId;
24 | }
25 | public void Serialize(CppBinaryWriter writer)
26 | {
27 | writer.Write(classId);
28 | }
29 | public int GetSize()
30 | {
31 | return 1;
32 | }
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/Nova.Bytecode/Codes/JumpIfFalseCode.cs:
--------------------------------------------------------------------------------
1 | using Nova.Utils.IO;
2 | using System;
3 | using System.Collections.Generic;
4 | using System.IO;
5 | using System.Linq;
6 | using System.Text;
7 | using System.Threading.Tasks;
8 |
9 | namespace Nova.ByteCode.Codes
10 | {
11 | public class JumpIfFalseCode : ICode
12 | {
13 | public int OpId => 6;
14 |
15 | public int targetIndex;
16 |
17 | public JumpIfFalseCode(int index)
18 | {
19 | this.targetIndex = index;
20 | }
21 |
22 | public override string ToString()
23 | {
24 | return "(" + OpId + ") " + "JumpIfFalse " + targetIndex;
25 | }
26 |
27 | public void Serialize(CppBinaryWriter writer)
28 | {
29 | writer.Write(targetIndex);
30 | }
31 | public int GetSize()
32 | {
33 | return 1;
34 | }
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/Nova.Bytecode/IO/MainPointEntry.cs:
--------------------------------------------------------------------------------
1 | using Nova.ByteCode.IO;
2 | using Nova.Utils.IO;
3 | using System;
4 | using System.Collections.Generic;
5 | using System.Linq;
6 | using System.Text;
7 | using System.Threading.Tasks;
8 |
9 | namespace Nova.Bytecode.IO
10 | {
11 | public class MainPointEntry : IByteElement
12 | {
13 | public int ClassIndex
14 | {
15 | get;
16 | set;
17 | }
18 | public int MethodIndex
19 | {
20 | get;
21 | set;
22 | }
23 | public MainPointEntry(int classIndex,int methodIndex)
24 | {
25 | this.ClassIndex = classIndex;
26 | this.MethodIndex = methodIndex;
27 | }
28 | public void Serialize(CppBinaryWriter writer)
29 | {
30 | writer.Write(ClassIndex);
31 | writer.Write(MethodIndex);
32 | }
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/Nova.Bytecode/Codes/PushConstCode.cs:
--------------------------------------------------------------------------------
1 | using Nova.ByteCode.Codes;
2 | using Nova.Utils.IO;
3 | using System;
4 | using System.Collections.Generic;
5 | using System.Linq;
6 | using System.Text;
7 | using System.Threading.Tasks;
8 |
9 | namespace Nova.Bytecode.Codes
10 | {
11 | public class PushConstCode : ICode
12 | {
13 | public int OpId => 14;
14 |
15 | private int constantId;
16 |
17 | public PushConstCode(int constantId)
18 | {
19 | this.constantId = constantId;
20 | }
21 |
22 | public override string ToString()
23 | {
24 | return "(" + OpId + ") " + "PushConst " + constantId;
25 | }
26 |
27 | public void Serialize(CppBinaryWriter writer)
28 | {
29 | writer.Write(constantId);
30 | }
31 | public int GetSize()
32 | {
33 | return 1;
34 | }
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/Nova.Bytecode/Codes/NativeCallCode.cs:
--------------------------------------------------------------------------------
1 | using Nova.Utils;
2 | using Nova.Utils.IO;
3 | using System;
4 | using System.Collections.Generic;
5 | using System.IO;
6 | using System.Linq;
7 | using System.Text;
8 | using System.Threading.Tasks;
9 |
10 | namespace Nova.ByteCode.Codes
11 | {
12 | public class NativeCallCode : ICode
13 | {
14 | public int OpId => 13;
15 |
16 | private int nativeId;
17 |
18 | public NativeCallCode(int nativeId)
19 | {
20 | this.nativeId = nativeId;
21 | }
22 | public override string ToString()
23 | {
24 | return "(" + OpId + ") " + "NativeId " + nativeId;
25 | }
26 |
27 | public void Serialize(CppBinaryWriter writer)
28 | {
29 | writer.Write(nativeId);
30 | }
31 |
32 | public int GetSize()
33 | {
34 | return 1;
35 | }
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/Nova.Bytecode/Codes/StructCallMethodCode.cs:
--------------------------------------------------------------------------------
1 | using Nova.ByteCode.Codes;
2 | using Nova.Utils.IO;
3 | using System;
4 | using System.Collections.Generic;
5 | using System.Linq;
6 | using System.Text;
7 | using System.Threading.Tasks;
8 |
9 | namespace Nova.Bytecode.Codes
10 | {
11 | public class StructCallMethodCode : ICode
12 | {
13 | public int OpId => 22;
14 |
15 | private int methodId;
16 |
17 | public StructCallMethodCode(int methodId)
18 | {
19 | this.methodId = methodId;
20 | }
21 | public override string ToString()
22 | {
23 | return "(" + OpId + ") " + "StructCallMethod " + methodId;
24 | }
25 | public void Serialize(CppBinaryWriter writer)
26 | {
27 | writer.Write(methodId);
28 | }
29 | public int GetSize()
30 | {
31 | return 1;
32 | }
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/Nova.Bytecode/Codes/ComparaisonCode.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.IO;
4 | using System.Linq;
5 | using System.Text;
6 | using System.Threading.Tasks;
7 | using Nova.ByteCode.Enums;
8 | using Nova.Utils.IO;
9 |
10 | namespace Nova.ByteCode.Codes
11 | {
12 | public class ComparaisonCode : ICode
13 | {
14 | private OperatorsEnum type;
15 |
16 | public int OpId => 2;
17 |
18 | public ComparaisonCode(OperatorsEnum type)
19 | {
20 | this.type = type;
21 | }
22 |
23 | public void Serialize(CppBinaryWriter writer)
24 | {
25 | writer.Write((int)type);
26 | }
27 |
28 | public override string ToString()
29 | {
30 | return "(" + OpId + ") " + "Comparaison" + type;
31 | }
32 |
33 | public int GetSize()
34 | {
35 | return 1;
36 | }
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/Nova/Types/NovaType.cs:
--------------------------------------------------------------------------------
1 | using Nova.Members;
2 | using System;
3 | using System.Collections.Generic;
4 | using System.Linq;
5 | using System.Text;
6 | using System.Threading.Tasks;
7 |
8 | namespace Nova.Types
9 | {
10 | public class NovaType
11 | {
12 | public bool IsPrimitive
13 | {
14 | get;
15 | set;
16 | }
17 |
18 | public string Name
19 | {
20 | get
21 | {
22 | return Class.ClassName;
23 | }
24 | }
25 |
26 | public Class Class
27 | {
28 | get;
29 | set;
30 | }
31 |
32 | public NovaType(Class @class, bool isPrimitive)
33 | {
34 | this.Class = @class;
35 | this.IsPrimitive = isPrimitive;
36 | }
37 |
38 | public override string ToString()
39 | {
40 | return "NovaType {" + Name + "}";
41 | }
42 |
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/Nova.Bytecode/Codes/StructLoadMemberCode.cs:
--------------------------------------------------------------------------------
1 | using Nova.ByteCode.Codes;
2 | using Nova.Utils.IO;
3 | using System;
4 | using System.Collections.Generic;
5 | using System.Linq;
6 | using System.Text;
7 | using System.Threading.Tasks;
8 |
9 | namespace Nova.Bytecode.Codes
10 | {
11 | public class StructLoadMemberCode : ICode
12 | {
13 | public int OpId => 24;
14 |
15 | private int propertyId; // propertyId (symbolTable)
16 |
17 | public StructLoadMemberCode(int propertyId)
18 | {
19 | this.propertyId = propertyId;
20 | }
21 |
22 | public void Serialize(CppBinaryWriter writer)
23 | {
24 | writer.Write(propertyId);
25 | }
26 |
27 | public override string ToString()
28 | {
29 | return "(" + OpId + ") " + "StructLoadMemberCode " + propertyId;
30 | }
31 | public int GetSize()
32 | {
33 | return 1;
34 | }
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/Nova.Bytecode/Codes/StructStoreMemberCode.cs:
--------------------------------------------------------------------------------
1 | using Nova.ByteCode.Codes;
2 | using Nova.Utils.IO;
3 | using System;
4 | using System.Collections.Generic;
5 | using System.Linq;
6 | using System.Text;
7 | using System.Threading.Tasks;
8 |
9 | namespace Nova.Bytecode.Codes
10 | {
11 | /*
12 | * this.Age = value
13 | */
14 | public class StructStoreMemberCode : ICode
15 | {
16 | public int OpId => 26;
17 |
18 | private int propertyId;
19 |
20 | public StructStoreMemberCode(int propertyId)
21 | {
22 | this.propertyId = propertyId;
23 | }
24 |
25 | public void Serialize(CppBinaryWriter writer)
26 | {
27 | writer.Write(propertyId);
28 | }
29 |
30 | public override string ToString()
31 | {
32 | return "(" + OpId + ") " + "StructStoreMember " + propertyId;
33 | }
34 | public int GetSize()
35 | {
36 | return 1;
37 | }
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/Nova.Utils/IO/CppBinaryWriter.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.IO;
4 | using System.Linq;
5 | using System.Text;
6 | using System.Threading.Tasks;
7 |
8 | namespace Nova.Utils.IO
9 | {
10 | public class CppBinaryWriter : BinaryWriter
11 | {
12 | public CppBinaryWriter(Stream output) : base(output)
13 | {
14 | }
15 |
16 | public CppBinaryWriter(Stream output, Encoding encoding) : base(output, encoding)
17 | {
18 | }
19 |
20 | public CppBinaryWriter(Stream output, Encoding encoding, bool leaveOpen) : base(output, encoding, leaveOpen)
21 | {
22 | }
23 |
24 | protected CppBinaryWriter()
25 | {
26 | }
27 |
28 | public override void Write(string value)
29 | {
30 | for (int i = 0; i < value.Length; i++)
31 | {
32 | this.Write(value[i]);
33 | }
34 | this.Write('\0');
35 | }
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/Nova.Utils/IO/CppBinaryReader.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.IO;
4 | using System.Linq;
5 | using System.Text;
6 | using System.Threading.Tasks;
7 |
8 | namespace Nova.Utils.IO
9 | {
10 | public class CppBinaryReader : BinaryReader
11 | {
12 | public CppBinaryReader(Stream input) : base(input)
13 | {
14 | }
15 |
16 | public CppBinaryReader(Stream input, Encoding encoding) : base(input, encoding)
17 | {
18 | }
19 |
20 | public CppBinaryReader(Stream input, Encoding encoding, bool leaveOpen) : base(input, encoding, leaveOpen)
21 | {
22 | }
23 |
24 | public override string ReadString()
25 | {
26 | string result = string.Empty;
27 |
28 | char read = this.ReadChar();
29 |
30 | while (read != '\0')
31 | {
32 | result += read;
33 | read = this.ReadChar();
34 | }
35 | return result;
36 | }
37 |
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/Nova.Bytecode/Codes/CtorCallCode.cs:
--------------------------------------------------------------------------------
1 | using Nova.ByteCode.Codes;
2 | using Nova.Utils.IO;
3 | using System;
4 | using System.Collections.Generic;
5 | using System.Linq;
6 | using System.Text;
7 | using System.Threading.Tasks;
8 |
9 | namespace Nova.Bytecode.Codes
10 | {
11 | public class CtorCallCode : ICode
12 | {
13 | public int OpId => 3;
14 |
15 | private int parametersCount;
16 |
17 | private int methodId;
18 |
19 | public CtorCallCode(int methodId, int parametersCount)
20 | {
21 | this.methodId = methodId;
22 | this.parametersCount = parametersCount;
23 | }
24 | public override string ToString()
25 | {
26 | return "(" + OpId + ") " + "CtorCall " + parametersCount;
27 | }
28 | public void Serialize(CppBinaryWriter writer)
29 | {
30 | writer.Write(parametersCount);
31 | writer.Write(methodId);
32 | }
33 | public int GetSize()
34 | {
35 | return 2;
36 | }
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/Nova.Bytecode/Codes/MethodCallCode.cs:
--------------------------------------------------------------------------------
1 | using Nova.ByteCode.Codes;
2 | using Nova.Utils.IO;
3 | using System;
4 | using System.Collections.Generic;
5 | using System.IO;
6 | using System.Linq;
7 | using System.Text;
8 | using System.Threading.Tasks;
9 |
10 | namespace Nova.Bytecode.Codes
11 | {
12 | public class MethodCallCode : ICode
13 | {
14 | public int OpId => 11;
15 |
16 | private int classId;
17 | private int methodId;
18 |
19 | public MethodCallCode(int classId, int methodId)
20 | {
21 | this.classId = classId;
22 | this.methodId = methodId;
23 | }
24 |
25 | public override string ToString()
26 | {
27 | return "(" + OpId + ") " + "MethodCall " + classId + " " + methodId;
28 | }
29 | public void Serialize(CppBinaryWriter writer)
30 | {
31 | writer.Write(classId);
32 | writer.Write(methodId);
33 | }
34 | public int GetSize()
35 | {
36 | return 2;
37 | }
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/Nova.Bytecode/Codes/StoreGlobalCode.cs:
--------------------------------------------------------------------------------
1 | using Nova.ByteCode.Codes;
2 | using Nova.Utils.IO;
3 | using System;
4 | using System.Collections.Generic;
5 | using System.IO;
6 | using System.Linq;
7 | using System.Text;
8 | using System.Threading.Tasks;
9 |
10 | namespace Nova.Bytecode.Codes
11 | {
12 | public class StoreGlobalCode : ICode
13 | {
14 | public int OpId => 21;
15 |
16 | private int classId;
17 |
18 | private int fieldId;
19 |
20 | public StoreGlobalCode(int classId,int fieldId)
21 | {
22 | this.classId = classId;
23 | this.fieldId = fieldId;
24 | }
25 |
26 | public override string ToString()
27 | {
28 | return "(" + OpId + ") " + "StoreGlobal " + classId + " " + fieldId;
29 | }
30 | public void Serialize(CppBinaryWriter writer)
31 | {
32 | writer.Write(classId);
33 | writer.Write(fieldId);
34 | }
35 | public int GetSize()
36 | {
37 | return 2;
38 | }
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/Nova.Antlr/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace Nova.Antlr
8 | {
9 | class Program
10 | {
11 | private const string LexerPath = "NovaLexer.g4";
12 | private const string ParserPath = "NovaParser.g4";
13 |
14 | private const string OutputPath = @"C:\Users\Skinz\Desktop\Nova\Compiler\Nova\Antlr";
15 |
16 | static void Main(string[] args)
17 | {
18 | AntlrTool tool = new AntlrTool("antlr4-csharp-4.6.6-complete.jar");
19 |
20 | bool lexer = tool.Generate(LexerPath, OutputPath);
21 | bool parser = tool.Generate(ParserPath, OutputPath);
22 |
23 | if (lexer && parser)
24 | {
25 | Console.WriteLine("Antlr files generated.");
26 | }
27 | else
28 | {
29 | Console.WriteLine("Antlr generation ended with errors...");
30 | }
31 | Console.Read();
32 | }
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/Nova/Expressions/ConstIntExpression.cs:
--------------------------------------------------------------------------------
1 | using Antlr4.Runtime;
2 | using Nova.Bytecode.Codes;
3 | using Nova.ByteCode.Codes;
4 | using Nova.ByteCode.Generation;
5 | using Nova.IO;
6 | using Nova.Members;
7 | using Nova.Semantics;
8 | using System;
9 | using System.Collections.Generic;
10 | using System.Linq;
11 | using System.Text;
12 | using System.Threading.Tasks;
13 |
14 | namespace Nova.Expressions
15 | {
16 | public class ConstIntExpression : Expression
17 | {
18 | private int Value
19 | {
20 | get;
21 | set;
22 | }
23 | public ConstIntExpression(IChild parent, ParserRuleContext context, int value) : base(parent, context)
24 | {
25 | this.Value = value;
26 | }
27 | public override void GenerateBytecode(ClassesContainer container, ByteBlock context)
28 | {
29 | context.Instructions.Add(new PushIntCode(Value));
30 | }
31 |
32 | public override void ValidateSemantics(SemanticsValidator validator)
33 | {
34 |
35 | }
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/Nova.Bytecode/Codes/LoadGlobalCode.cs:
--------------------------------------------------------------------------------
1 | using Nova.ByteCode;
2 | using Nova.ByteCode.Codes;
3 | using Nova.Utils.IO;
4 | using System;
5 | using System.Collections.Generic;
6 | using System.IO;
7 | using System.Linq;
8 | using System.Text;
9 | using System.Threading.Tasks;
10 |
11 | namespace Nova.Bytecode.Codes
12 | {
13 | public class LoadGlobalCode : ICode
14 | {
15 | public int OpId => 8;
16 |
17 | private int classId;
18 |
19 | private int fieldId;
20 |
21 | public LoadGlobalCode(int classId, int fieldId)
22 | {
23 | this.classId = classId;
24 | this.fieldId = fieldId;
25 | }
26 |
27 | public override string ToString()
28 | {
29 | return "(" + OpId + ") " + "LoadGlobal " + classId + " " + fieldId;
30 | }
31 | public void Serialize(CppBinaryWriter writer)
32 | {
33 | writer.Write(classId);
34 | writer.Write(fieldId);
35 | }
36 | public int GetSize()
37 | {
38 | return 2;
39 | }
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/Nova/Semantics/SemanticalError.cs:
--------------------------------------------------------------------------------
1 | using Antlr4.Runtime;
2 | using System;
3 | using System.Collections.Generic;
4 | using System.IO;
5 | using System.Linq;
6 | using System.Text;
7 | using System.Threading.Tasks;
8 |
9 | namespace Nova.Semantics
10 | {
11 | public class SemanticalError
12 | {
13 | private string Filepath
14 | {
15 | get;
16 | set;
17 | }
18 | private string Message
19 | {
20 | get;
21 | set;
22 | }
23 | private ParserRuleContext Context
24 | {
25 | get;
26 | set;
27 | }
28 | public SemanticalError(string filepath, string message, ParserRuleContext context)
29 | {
30 | this.Filepath = filepath;
31 | this.Message = message;
32 | this.Context = context; // starting from 1
33 | }
34 | public override string ToString()
35 | {
36 | return "File: " + Path.GetFileName(Filepath) + " " + Message + " at line " + Context.start.Line;
37 | }
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/Nova/Statements/EmptyStatement.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.IO;
4 | using System.Linq;
5 | using System.Text;
6 | using System.Threading.Tasks;
7 | using Nova.ByteCode.Codes;
8 | using Nova.ByteCode.Generation;
9 | using Nova.Lexer;
10 | using Nova.IO;
11 | using Nova.Members;
12 | using Nova.Semantics;
13 | using Antlr4.Runtime;
14 |
15 | namespace Nova.Statements
16 | {
17 | public class EmptyStatement : Statement
18 | {
19 | public EmptyStatement(IChild member, ParserRuleContext context) : base(member, context)
20 | {
21 |
22 | }
23 |
24 | public override void GenerateBytecode(ClassesContainer container, ByteBlock context)
25 | {
26 | throw new NotImplementedException();
27 | }
28 |
29 | public override void ValidateSemantics(SemanticsValidator validator)
30 | {
31 | throw new NotImplementedException();
32 | }
33 |
34 | public override void ValidateTypes(SemanticsValidator validator)
35 | {
36 | throw new NotImplementedException();
37 | }
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/Nova/Expressions/ConstStringExpression.cs:
--------------------------------------------------------------------------------
1 | using Antlr4.Runtime;
2 | using Nova.Bytecode.Codes;
3 | using Nova.ByteCode.Generation;
4 | using Nova.IO;
5 | using Nova.Members;
6 | using Nova.Semantics;
7 | using System;
8 | using System.Collections.Generic;
9 | using System.Linq;
10 | using System.Text;
11 | using System.Threading.Tasks;
12 |
13 | namespace Nova.Expressions
14 | {
15 | public class ConstStringExpression : Expression
16 | {
17 | private string Value
18 | {
19 | get;
20 | set;
21 | }
22 | public ConstStringExpression(IChild parent, ParserRuleContext context, string value) : base(parent, context)
23 | {
24 | this.Value = value;
25 | }
26 | public override void GenerateBytecode(ClassesContainer container, ByteBlock context)
27 | {
28 | int constantId = context.BindConstant(Value);
29 | context.Instructions.Add(new PushConstCode(constantId));
30 | }
31 |
32 | public override void ValidateSemantics(SemanticsValidator validator)
33 | {
34 |
35 | }
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/Nova/Expressions/ConstBoolExpression.cs:
--------------------------------------------------------------------------------
1 | using Antlr4.Runtime;
2 | using Nova.Bytecode.Codes;
3 | using Nova.ByteCode.Codes;
4 | using Nova.ByteCode.Generation;
5 | using Nova.IO;
6 | using Nova.Members;
7 | using Nova.Semantics;
8 | using System;
9 | using System.Collections.Generic;
10 | using System.Linq;
11 | using System.Text;
12 | using System.Threading.Tasks;
13 |
14 | namespace Nova.Expressions
15 | {
16 | public class ConstBoolExpression : Expression
17 | {
18 | private bool Value
19 | {
20 | get;
21 | set;
22 | }
23 | public ConstBoolExpression(IChild parent, ParserRuleContext context, bool value) : base(parent, context)
24 | {
25 | this.Value = value;
26 | }
27 | public override void GenerateBytecode(ClassesContainer container, ByteBlock context)
28 | {
29 | int value = Value == true ? 1 : 0;
30 | context.Instructions.Add(new PushIntCode(value)); // rather push byte !
31 | }
32 |
33 | public override void ValidateSemantics(SemanticsValidator validator)
34 | {
35 |
36 | }
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/Nova/Expressions/Expression.cs:
--------------------------------------------------------------------------------
1 | using Antlr4.Runtime;
2 | using Nova.ByteCode.Generation;
3 | using Nova.IO;
4 | using Nova.Members;
5 | using Nova.Semantics;
6 | using System;
7 | using System.Collections.Generic;
8 | using System.Linq;
9 | using System.Text;
10 | using System.Threading.Tasks;
11 |
12 | namespace Nova.Expressions
13 | {
14 | public abstract class Expression : ISemanticMember, IChild
15 | {
16 | public Class ParentClass => Parent.ParentClass;
17 |
18 | public IChild Parent
19 | {
20 | get;
21 | private set;
22 | }
23 | public ParserRuleContext ParsingContext
24 | {
25 | get;
26 | private set;
27 | }
28 | public Expression(IChild parent,ParserRuleContext context)
29 | {
30 | this.Parent = parent;
31 | this.ParsingContext = context;
32 | }
33 | public abstract void GenerateBytecode(ClassesContainer container, ByteBlock context);
34 |
35 | public abstract void ValidateSemantics(SemanticsValidator validator);
36 |
37 | public override string ToString()
38 | {
39 | return this.GetType().Name;
40 | }
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/Nova/Statements/ReturnStatement.cs:
--------------------------------------------------------------------------------
1 | using Nova.ByteCode.Codes;
2 | using Nova.ByteCode.Generation;
3 | using Nova.Lexer;
4 | using Nova.IO;
5 | using Nova.Members;
6 | using Nova.Semantics;
7 | using System;
8 | using System.Collections.Generic;
9 | using System.IO;
10 | using System.Linq;
11 | using System.Text;
12 | using System.Text.RegularExpressions;
13 | using System.Threading.Tasks;
14 | using Antlr4.Runtime;
15 |
16 | namespace Nova.Statements
17 | {
18 | public class ReturnStatement : Statement
19 | {
20 | public ExpressionNode Value
21 | {
22 | get;
23 | set;
24 | }
25 | public ReturnStatement(IChild parent, ParserRuleContext context) : base(parent, context)
26 | {
27 |
28 | }
29 |
30 |
31 | public override void GenerateBytecode(ClassesContainer container, ByteBlock context)
32 | {
33 | if (!Value.Empty)
34 | {
35 | Value.GenerateBytecode(container, context);
36 | }
37 | context.Instructions.Add(new ReturnCode());
38 | }
39 |
40 | public override void ValidateSemantics(SemanticsValidator validator)
41 | {
42 | Value.ValidateSemantics(validator);
43 | }
44 |
45 | public override void ValidateTypes(SemanticsValidator validator)
46 | {
47 |
48 | }
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/Nova/Statements/Statement.cs:
--------------------------------------------------------------------------------
1 | using Nova.ByteCode.Codes;
2 | using Nova.ByteCode.Generation;
3 | using Nova.ByteCode.IO;
4 | using Nova.Lexer;
5 | using Nova.IO;
6 | using Nova.Members;
7 | using Nova.Semantics;
8 | using System;
9 | using System.Collections.Generic;
10 | using System.IO;
11 | using System.Linq;
12 | using System.Text;
13 | using System.Threading.Tasks;
14 | using Nova.Bytecode.Symbols;
15 | using Nova.Bytecode.Enums;
16 | using Antlr4.Runtime;
17 |
18 | namespace Nova.Statements
19 | {
20 | public abstract class Statement : ISemanticMember , IChild
21 | {
22 | public IChild Parent
23 | {
24 | get;
25 | private set;
26 | }
27 |
28 | public ParserRuleContext ParsingContext
29 | {
30 | get;
31 | private set;
32 | }
33 |
34 | public Class ParentClass => Parent.ParentClass;
35 |
36 | public Statement(IChild parent, ParserRuleContext ruleContext)
37 | {
38 | this.Parent = parent;
39 | this.ParsingContext = ruleContext;
40 | }
41 |
42 | public override string ToString()
43 | {
44 | return string.Format("({0}) {1}", this.GetType().Name, ParsingContext.GetText());
45 | }
46 |
47 | public abstract void GenerateBytecode(ClassesContainer container, ByteBlock context);
48 |
49 | public abstract void ValidateSemantics(SemanticsValidator validator);
50 |
51 | public abstract void ValidateTypes(SemanticsValidator validator);
52 |
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/Nova.Bytecode/Symbols/SymbolTable.cs:
--------------------------------------------------------------------------------
1 | using Nova.ByteCode.IO;
2 | using Nova.Utils.IO;
3 | using System;
4 | using System.Collections.Generic;
5 | using System.Linq;
6 | using System.Text;
7 | using System.Threading.Tasks;
8 |
9 | namespace Nova.Bytecode.Symbols
10 | {
11 | public class SymbolTable : IByteElement
12 | {
13 | private Dictionary m_table;
14 |
15 | public int Count
16 | {
17 | get
18 | {
19 | return m_table.Count;
20 | }
21 | }
22 | public SymbolTable()
23 | {
24 | this.m_table = new Dictionary();
25 | }
26 | public Symbol GetSymbol(string name)
27 | {
28 | Symbol sym = null;
29 |
30 | if (m_table.TryGetValue(name, out sym))
31 | {
32 | return sym;
33 | }
34 | else
35 | {
36 | return null;
37 | }
38 | }
39 | public int Bind(string name,string type)
40 | {
41 | int id = (m_table.Count - 1) + 1;
42 | m_table.Add(name, new Symbol(id, type));
43 | return id;
44 | }
45 | public void Serialize(CppBinaryWriter writer)
46 | {
47 | writer.Write(m_table.Count);
48 |
49 | foreach (var symbol in m_table)
50 | {
51 | writer.Write(symbol.Key);
52 | symbol.Value.Serialize(writer);
53 | }
54 | }
55 |
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/Nova.Bytecode/IO/ByteField.cs:
--------------------------------------------------------------------------------
1 | using Nova.ByteCode.Codes;
2 | using Nova.ByteCode.Enums;
3 | using Nova.ByteCode.Generation;
4 | using Nova.Utils.IO;
5 | using System;
6 | using System.Collections.Generic;
7 | using System.IO;
8 | using System.Linq;
9 | using System.Text;
10 | using System.Threading.Tasks;
11 |
12 | namespace Nova.ByteCode.IO
13 | {
14 | ///
15 | /// Field static
16 | ///
17 | public class ByteField : IByteElement
18 | {
19 | public string Name
20 | {
21 | get;
22 | set;
23 | }
24 | private ByteBlock Meta
25 | {
26 | get;
27 | set;
28 | }
29 | public object Value
30 | {
31 | get;
32 | set;
33 | }
34 | public ByteClass ParentClass
35 | {
36 | get;
37 | set;
38 | }
39 | public ModifiersEnum Modifiers
40 | {
41 | get;
42 | private set;
43 | }
44 | public ByteField(ByteClass parentClass, ModifiersEnum modifiers, string name, ByteBlock meta)
45 | {
46 | this.ParentClass = parentClass;
47 | this.Name = name;
48 | this.Meta = meta;
49 | this.Modifiers = modifiers;
50 | }
51 |
52 | public void Serialize(CppBinaryWriter writer)
53 | {
54 | writer.Write(Name);
55 | writer.Write((byte)Modifiers);
56 | Meta.Serialize(writer);
57 | }
58 |
59 |
60 |
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/Nova/Antlr/NovaLexer.tokens:
--------------------------------------------------------------------------------
1 | USING=1
2 | CLASS=2
3 | STRUCT=3
4 | PUBLIC=4
5 | PRIVATE=5
6 | PRIMITIVE=6
7 | UNIT=7
8 | DOUBLE=8
9 | FLOAT=9
10 | INT=10
11 | SHORT=11
12 | STRING=12
13 | BOOLEAN=13
14 | CHAR=14
15 | BYTE=15
16 | LONG=16
17 | FOR=17
18 | IF=18
19 | WHILE=19
20 | ELSE=20
21 | RETURN=21
22 | NEW=22
23 | NATIVE=23
24 | ASSIGN=24
25 | ADD=25
26 | SUB=26
27 | MUL=27
28 | DIV=28
29 | LE=29
30 | GE=30
31 | GT=31
32 | LT=32
33 | EQUAL=33
34 | NOTEQUAL=34
35 | AND=35
36 | OR=36
37 | LPAREN=37
38 | RPAREN=38
39 | LBRACE=39
40 | RBRACE=40
41 | LBRACK=41
42 | RBRACK=42
43 | COMMA=43
44 | DOT=44
45 | SEMI=45
46 | NULL_LITERAL=46
47 | BOOL_LITERAL=47
48 | IDENTIFIER=48
49 | WS=49
50 | COMMENT=50
51 | LINE_COMMENT=51
52 | DECIMAL_LITERAL=52
53 | HEX_LITERAL=53
54 | OCT_LITERAL=54
55 | BINARY_LITERAL=55
56 | FLOAT_LITERAL=56
57 | HEX_FLOAT_LITERAL=57
58 | CHAR_LITERAL=58
59 | STRING_LITERAL=59
60 | 'using'=1
61 | 'class'=2
62 | 'struct'=3
63 | 'public'=4
64 | 'private'=5
65 | 'primitive'=6
66 | 'unit'=7
67 | 'double'=8
68 | 'float'=9
69 | 'int'=10
70 | 'short'=11
71 | 'string'=12
72 | 'bool'=13
73 | 'char'=14
74 | 'byte'=15
75 | 'long'=16
76 | 'for'=17
77 | 'if'=18
78 | 'while'=19
79 | 'else'=20
80 | 'return'=21
81 | '->'=22
82 | '~'=23
83 | '='=24
84 | '+'=25
85 | '-'=26
86 | '*'=27
87 | '/'=28
88 | '<='=29
89 | '>='=30
90 | '>'=31
91 | '<'=32
92 | '=='=33
93 | '!='=34
94 | '&&'=35
95 | '||'=36
96 | '('=37
97 | ')'=38
98 | '{'=39
99 | '}'=40
100 | '['=41
101 | ']'=42
102 | ','=43
103 | '.'=44
104 | ';'=45
105 | 'null'=46
106 |
--------------------------------------------------------------------------------
/Nova/Antlr/NovaParser.tokens:
--------------------------------------------------------------------------------
1 | USING=1
2 | CLASS=2
3 | STRUCT=3
4 | PUBLIC=4
5 | PRIVATE=5
6 | PRIMITIVE=6
7 | UNIT=7
8 | DOUBLE=8
9 | FLOAT=9
10 | INT=10
11 | SHORT=11
12 | STRING=12
13 | BOOLEAN=13
14 | CHAR=14
15 | BYTE=15
16 | LONG=16
17 | FOR=17
18 | IF=18
19 | WHILE=19
20 | ELSE=20
21 | RETURN=21
22 | NEW=22
23 | NATIVE=23
24 | ASSIGN=24
25 | ADD=25
26 | SUB=26
27 | MUL=27
28 | DIV=28
29 | LE=29
30 | GE=30
31 | GT=31
32 | LT=32
33 | EQUAL=33
34 | NOTEQUAL=34
35 | AND=35
36 | OR=36
37 | LPAREN=37
38 | RPAREN=38
39 | LBRACE=39
40 | RBRACE=40
41 | LBRACK=41
42 | RBRACK=42
43 | COMMA=43
44 | DOT=44
45 | SEMI=45
46 | NULL_LITERAL=46
47 | BOOL_LITERAL=47
48 | IDENTIFIER=48
49 | WS=49
50 | COMMENT=50
51 | LINE_COMMENT=51
52 | DECIMAL_LITERAL=52
53 | HEX_LITERAL=53
54 | OCT_LITERAL=54
55 | BINARY_LITERAL=55
56 | FLOAT_LITERAL=56
57 | HEX_FLOAT_LITERAL=57
58 | CHAR_LITERAL=58
59 | STRING_LITERAL=59
60 | 'using'=1
61 | 'class'=2
62 | 'struct'=3
63 | 'public'=4
64 | 'private'=5
65 | 'primitive'=6
66 | 'unit'=7
67 | 'double'=8
68 | 'float'=9
69 | 'int'=10
70 | 'short'=11
71 | 'string'=12
72 | 'bool'=13
73 | 'char'=14
74 | 'byte'=15
75 | 'long'=16
76 | 'for'=17
77 | 'if'=18
78 | 'while'=19
79 | 'else'=20
80 | 'return'=21
81 | '->'=22
82 | '~'=23
83 | '='=24
84 | '+'=25
85 | '-'=26
86 | '*'=27
87 | '/'=28
88 | '<='=29
89 | '>='=30
90 | '>'=31
91 | '<'=32
92 | '=='=33
93 | '!='=34
94 | '&&'=35
95 | '||'=36
96 | '('=37
97 | ')'=38
98 | '{'=39
99 | '}'=40
100 | '['=41
101 | ']'=42
102 | ','=43
103 | '.'=44
104 | ';'=45
105 | 'null'=46
106 |
--------------------------------------------------------------------------------
/Nova/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // Les informations générales relatives à un assembly dépendent de
6 | // l'ensemble d'attributs suivant. Changez les valeurs de ces attributs pour modifier les informations
7 | // associées à un assembly.
8 | [assembly: AssemblyTitle("Nova")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Nova")]
13 | [assembly: AssemblyCopyright("Copyright © 2020")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // L'affectation de la valeur false à ComVisible rend les types invisibles dans cet assembly
18 | // aux composants COM. Si vous devez accéder à un type dans cet assembly à partir de
19 | // COM, affectez la valeur true à l'attribut ComVisible sur ce type.
20 | [assembly: ComVisible(false)]
21 |
22 | // Le GUID suivant est pour l'ID de la typelib si ce projet est exposé à COM
23 | [assembly: Guid("8ef17b2e-af37-444c-b2c0-3b6099321bd7")]
24 |
25 | // Les informations de version pour un assembly se composent des quatre valeurs suivantes :
26 | //
27 | // Version principale
28 | // Version secondaire
29 | // Numéro de build
30 | // Révision
31 | //
32 | // Vous pouvez spécifier toutes les valeurs ou indiquer les numéros de build et de révision par défaut
33 | // en utilisant '*', comme indiqué ci-dessous :
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Nova.Antlr/AntlrTool.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Diagnostics;
4 | using System.Linq;
5 | using System.Text;
6 | using System.Threading.Tasks;
7 |
8 | namespace Nova.Antlr
9 | {
10 | public class AntlrTool
11 | {
12 | public const string DLanguage = "CSharp_v4_5";
13 |
14 | public const string AntlrNamespace = "org.antlr.v4.Tool";
15 |
16 | private string AntlrPath
17 | {
18 | get;
19 | set;
20 | }
21 | public AntlrTool(string antlrPath)
22 | {
23 | this.AntlrPath = antlrPath;
24 | }
25 | public bool Generate(string grammarPath, string outputDirectory)
26 | {
27 | ProcessStartInfo psi = new ProcessStartInfo("java", string.Format("-cp {0} {1} -Dlanguage={2} {3} -o {4}", AntlrPath, AntlrNamespace, DLanguage, grammarPath, outputDirectory));
28 | Process process = new Process();
29 | process.StartInfo = psi;
30 | process.StartInfo.UseShellExecute = false;
31 | process.StartInfo.RedirectStandardOutput = true;
32 | process.OutputDataReceived += Process_OutputDataReceived;
33 | process.Start();
34 | process.BeginOutputReadLine();
35 | process.WaitForExit();
36 | return process.ExitCode == 0;
37 | }
38 |
39 | private void Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
40 | {
41 | if (!string.IsNullOrWhiteSpace(e.Data))
42 | Console.WriteLine(e.Data);
43 | }
44 |
45 |
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/Nova/Expressions/VariableNameExpression.cs:
--------------------------------------------------------------------------------
1 | using Antlr4.Runtime;
2 | using Nova.Bytecode.Codes;
3 | using Nova.Bytecode.Symbols;
4 | using Nova.ByteCode.Codes;
5 | using Nova.ByteCode.Generation;
6 | using Nova.IO;
7 | using Nova.Members;
8 | using Nova.Semantics;
9 | using System;
10 | using System.Collections.Generic;
11 | using System.Linq;
12 | using System.Text;
13 | using System.Threading.Tasks;
14 | using Nova.Lexer;
15 | using Nova.Bytecode.Enums;
16 | using Nova.Expressions.Accessors;
17 |
18 | namespace Nova.Expressions
19 | {
20 | public class VariableNameExpression : AccessorExpression
21 | {
22 | public override AccessorType AccessorType => AccessorType.Field;
23 |
24 | public AccessorTree Tree
25 | {
26 | get;
27 | private set;
28 | }
29 | public bool Store
30 | {
31 | get;
32 | set;
33 | }
34 |
35 |
36 | public VariableNameExpression(IChild parent, ParserRuleContext context) : base(parent, context)
37 | {
38 |
39 | }
40 | public override void GenerateBytecode(ClassesContainer container, ByteBlock context)
41 | {
42 | Tree.GenerateBytecode(container, context);
43 | }
44 |
45 |
46 | public override void ValidateSemantics(SemanticsValidator validator)
47 | {
48 | this.Tree = new AccessorTree(this, Store);
49 | this.Tree.ValidateSemantics(validator);
50 | }
51 | public override string ToString()
52 | {
53 | return base.ToString() + " {" + Name + "}";
54 | }
55 |
56 |
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/Nova.Antlr/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // Les informations générales relatives à un assembly dépendent de
6 | // l'ensemble d'attributs suivant. Changez les valeurs de ces attributs pour modifier les informations
7 | // associées à un assembly.
8 | [assembly: AssemblyTitle("Nova.Antlr")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Nova.Antlr")]
13 | [assembly: AssemblyCopyright("Copyright © 2020")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // L'affectation de la valeur false à ComVisible rend les types invisibles dans cet assembly
18 | // aux composants COM. Si vous devez accéder à un type dans cet assembly à partir de
19 | // COM, affectez la valeur true à l'attribut ComVisible sur ce type.
20 | [assembly: ComVisible(false)]
21 |
22 | // Le GUID suivant est pour l'ID de la typelib si ce projet est exposé à COM
23 | [assembly: Guid("0218b638-6ff7-4859-aa0d-0cdf4ab781f8")]
24 |
25 | // Les informations de version pour un assembly se composent des quatre valeurs suivantes :
26 | //
27 | // Version principale
28 | // Version secondaire
29 | // Numéro de build
30 | // Révision
31 | //
32 | // Vous pouvez spécifier toutes les valeurs ou indiquer les numéros de build et de révision par défaut
33 | // en utilisant '*', comme indiqué ci-dessous :
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Nova.Utils/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // Les informations générales relatives à un assembly dépendent de
6 | // l'ensemble d'attributs suivant. Changez les valeurs de ces attributs pour modifier les informations
7 | // associées à un assembly.
8 | [assembly: AssemblyTitle("Nova.Utils")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Nova.Utils")]
13 | [assembly: AssemblyCopyright("Copyright © 2020")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // L'affectation de la valeur false à ComVisible rend les types invisibles dans cet assembly
18 | // aux composants COM. Si vous devez accéder à un type dans cet assembly à partir de
19 | // COM, affectez la valeur true à l'attribut ComVisible sur ce type.
20 | [assembly: ComVisible(false)]
21 |
22 | // Le GUID suivant est pour l'ID de la typelib si ce projet est exposé à COM
23 | [assembly: Guid("7350e0c1-89ba-49ca-a71f-635916446c35")]
24 |
25 | // Les informations de version pour un assembly se composent des quatre valeurs suivantes :
26 | //
27 | // Version principale
28 | // Version secondaire
29 | // Numéro de build
30 | // Révision
31 | //
32 | // Vous pouvez spécifier toutes les valeurs ou indiquer les numéros de build et de révision par défaut
33 | // en utilisant '*', comme indiqué ci-dessous :
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Nova.Bytecode/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // Les informations générales relatives à un assembly dépendent de
6 | // l'ensemble d'attributs suivant. Changez les valeurs de ces attributs pour modifier les informations
7 | // associées à un assembly.
8 | [assembly: AssemblyTitle("Nova.Bytecode")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Nova.Bytecode")]
13 | [assembly: AssemblyCopyright("Copyright © 2020")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // L'affectation de la valeur false à ComVisible rend les types invisibles dans cet assembly
18 | // aux composants COM. Si vous devez accéder à un type dans cet assembly à partir de
19 | // COM, affectez la valeur true à l'attribut ComVisible sur ce type.
20 | [assembly: ComVisible(false)]
21 |
22 | // Le GUID suivant est pour l'ID de la typelib si ce projet est exposé à COM
23 | [assembly: Guid("938e11e6-2ace-445a-bdf0-14715e209e6d")]
24 |
25 | // Les informations de version pour un assembly se composent des quatre valeurs suivantes :
26 | //
27 | // Version principale
28 | // Version secondaire
29 | // Numéro de build
30 | // Révision
31 | //
32 | // Vous pouvez spécifier toutes les valeurs ou indiquer les numéros de build et de révision par défaut
33 | // en utilisant '*', comme indiqué ci-dessous :
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Nova.Compiler/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // Les informations générales relatives à un assembly dépendent de
6 | // l'ensemble d'attributs suivant. Changez les valeurs de ces attributs pour modifier les informations
7 | // associées à un assembly.
8 | [assembly: AssemblyTitle("Nova.Builder")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Nova.Builder")]
13 | [assembly: AssemblyCopyright("Copyright © 2020")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // L'affectation de la valeur false à ComVisible rend les types invisibles dans cet assembly
18 | // aux composants COM. Si vous devez accéder à un type dans cet assembly à partir de
19 | // COM, affectez la valeur true à l'attribut ComVisible sur ce type.
20 | [assembly: ComVisible(false)]
21 |
22 | // Le GUID suivant est pour l'ID de la typelib si ce projet est exposé à COM
23 | [assembly: Guid("02d71f87-48c5-4ee2-83cb-3ffd8b79e877")]
24 |
25 | // Les informations de version pour un assembly se composent des quatre valeurs suivantes :
26 | //
27 | // Version principale
28 | // Version secondaire
29 | // Numéro de build
30 | // Révision
31 | //
32 | // Vous pouvez spécifier toutes les valeurs ou indiquer les numéros de build et de révision par défaut
33 | // en utilisant '*', comme indiqué ci-dessous :
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Nova/Members/Variable.cs:
--------------------------------------------------------------------------------
1 | using Antlr4.Runtime;
2 | using Nova.IO;
3 | using Nova.Semantics;
4 | using Nova.Types;
5 | using System;
6 | using System.Collections.Generic;
7 | using System.IO;
8 | using System.Linq;
9 | using System.Text;
10 | using System.Threading.Tasks;
11 |
12 | namespace Nova.Members
13 | {
14 | public class Variable : IAccessible
15 | {
16 | public string Name
17 | {
18 | get;
19 | set;
20 | }
21 | public string RawType
22 | {
23 | get;
24 | set;
25 | }
26 | public NovaType Type
27 | {
28 | get;
29 | set;
30 | }
31 | private ParserRuleContext Context
32 | {
33 | get;
34 | set;
35 | }
36 | public Variable(string name, string type, ParserRuleContext context)
37 | {
38 | this.Name = name;
39 | this.RawType = type;
40 | this.Context = context;
41 | }
42 |
43 | public void ValidateTypes(SemanticsValidator validator)
44 | {
45 | this.Type = validator.Container.TypeManager.GetTypeInstance(RawType);
46 |
47 | if (this.Type == null)
48 | {
49 | validator.AddError("Unknown variable type " + RawType, Context);
50 | }
51 |
52 | }
53 |
54 | public override string ToString()
55 | {
56 | return RawType + " " + Name;
57 | }
58 |
59 | public Class GetContextualClass(SemanticsValidator validator)
60 | {
61 | return Type != null ? Type.Class : null;
62 | }
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/Nova.Bytecode/IO/Symbol.cs:
--------------------------------------------------------------------------------
1 | using Nova.Utils.IO;
2 | using System;
3 | using System.Collections.Generic;
4 | using System.Linq;
5 | using System.Text;
6 | using System.Threading.Tasks;
7 |
8 | namespace Nova.Bytecode.IO
9 | {
10 | public class Symbol
11 | {
12 | public int Id;
13 |
14 | public Symbol(int id)
15 | {
16 | this.Id = id;
17 | }
18 | public void Serialize(CppBinaryWriter writer)
19 | {
20 | writer.Write(Id);
21 | }
22 | }
23 | public class SymbolTable
24 | {
25 | private Dictionary Symbols
26 | {
27 | get;
28 | set;
29 | }
30 |
31 | public SymbolTable()
32 | {
33 | this.Symbols = new Dictionary();
34 | }
35 |
36 | public int BindVariable(string name)
37 | {
38 | int id = (Symbols.Count - 1) + 1;
39 | Symbols.Add(name, new Symbol(id));
40 | return id;
41 | }
42 | public int GetVariableId(string name)
43 | {
44 | Symbol sym = null;
45 |
46 | if (Symbols.TryGetValue(name, out sym))
47 | {
48 | return sym.Id;
49 | }
50 | else
51 | {
52 | return -1;
53 | }
54 | }
55 | public void Serialize(CppBinaryWriter writer)
56 | {
57 | writer.Write(Symbols.Count);
58 |
59 | foreach (var pair in Symbols)
60 | {
61 | writer.Write(pair.Key);
62 | pair.Value.Serialize(writer);
63 | }
64 | }
65 |
66 |
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/Nova/Statements/DeclarationStatement.cs:
--------------------------------------------------------------------------------
1 | using Nova.ByteCode.Codes;
2 | using Nova.ByteCode.Generation;
3 | using Nova.Lexer;
4 | using Nova.IO;
5 | using Nova.Members;
6 | using Nova.Semantics;
7 | using System;
8 | using System.Collections.Generic;
9 | using System.IO;
10 | using System.Linq;
11 | using System.Text;
12 | using System.Text.RegularExpressions;
13 | using System.Threading.Tasks;
14 | using Antlr4.Runtime;
15 |
16 | namespace Nova.Statements
17 | {
18 | public class DeclarationStatement : Statement
19 | {
20 | public Variable Variable
21 | {
22 | get;
23 | set;
24 | }
25 | public ExpressionNode Value
26 | {
27 | get;
28 | set;
29 | }
30 | public DeclarationStatement(IChild parent, ParserRuleContext context) : base(parent, context)
31 | {
32 |
33 | }
34 |
35 |
36 | public override void GenerateBytecode(ClassesContainer container, ByteBlock context)
37 | {
38 | List results = new List();
39 |
40 | int variableId = context.SymbolTable.Bind(Variable.Name, Variable.RawType);
41 |
42 | Value.GenerateBytecode(container, context);
43 | context.Instructions.Add(new StoreCode(variableId));
44 | }
45 |
46 | public override void ValidateSemantics(SemanticsValidator validator)
47 | {
48 | Value.ValidateSemantics(validator);
49 | validator.DeclareVariable(Variable);
50 |
51 | }
52 |
53 | public override void ValidateTypes(SemanticsValidator validator)
54 | {
55 | Variable.ValidateTypes(validator);
56 | }
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/Nova/Statements/ExpressionStatement.cs:
--------------------------------------------------------------------------------
1 | using Antlr4.Runtime;
2 | using Nova.ByteCode.Generation;
3 | using Nova.Expressions;
4 | using Nova.IO;
5 | using Nova.Lexer;
6 | using Nova.Members;
7 | using Nova.Semantics;
8 | using System;
9 | using System.Collections.Generic;
10 | using System.Linq;
11 | using System.Net;
12 | using System.Text;
13 | using System.Threading.Tasks;
14 |
15 | namespace Nova.Statements
16 | {
17 | public class ExpressionStatement : Statement
18 | {
19 | public ExpressionNode Expression
20 | {
21 | get;
22 | set;
23 | }
24 | public ExpressionStatement(IChild parent, ParserRuleContext ruleContext) : base(parent, ruleContext)
25 | {
26 |
27 | }
28 |
29 | public override void GenerateBytecode(ClassesContainer container, ByteBlock context)
30 | {
31 | Expression.GenerateBytecode(container, context);
32 | }
33 |
34 | public override void ValidateSemantics(SemanticsValidator validator)
35 | {
36 | var tree = Expression.GetTree();
37 |
38 | foreach (var expr in tree)
39 | {
40 | if (expr is not MethodCallExpression && expr is not NativeCallExpression && expr is not VariableNameExpression)
41 | {
42 | validator.AddError("Forbidenn expression statement (" + expr.GetType().Name + ")", base.ParsingContext);
43 | return;
44 | }
45 | }
46 |
47 |
48 | Expression.ValidateSemantics(validator);
49 | }
50 |
51 | public override void ValidateTypes(SemanticsValidator validator)
52 | {
53 |
54 | }
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/Nova/Statements/AssignationStatement.cs:
--------------------------------------------------------------------------------
1 | using Nova.ByteCode.Codes;
2 | using Nova.ByteCode.Generation;
3 | using Nova.Lexer;
4 | using Nova.IO;
5 | using Nova.Members;
6 | using Nova.Semantics;
7 | using System;
8 | using System.Collections.Generic;
9 | using System.IO;
10 | using System.Linq;
11 | using System.Text;
12 | using System.Text.RegularExpressions;
13 | using System.Threading.Tasks;
14 | using Nova.Bytecode.Codes;
15 | using Nova.Bytecode.Enums;
16 | using Nova.Bytecode.Symbols;
17 | using Antlr4.Runtime;
18 | using Nova.Expressions;
19 |
20 | namespace Nova.Statements
21 | {
22 | public class AssignationStatement : Statement
23 | {
24 | public VariableNameExpression Target
25 | {
26 | get;
27 | set;
28 | }
29 |
30 | public char Operator
31 | {
32 | get;
33 | set;
34 | }
35 | public ExpressionNode Value
36 | {
37 | get;
38 | set;
39 | }
40 |
41 | public AssignationStatement(IChild parent, ParserRuleContext context) : base(parent, context)
42 | {
43 |
44 | }
45 |
46 | public override void GenerateBytecode(ClassesContainer container, ByteBlock context)
47 | {
48 | Value.GenerateBytecode(container, context);
49 |
50 | Target.GenerateBytecode(container, context);
51 |
52 | }
53 |
54 | public override void ValidateSemantics(SemanticsValidator validator)
55 | {
56 | Target.Store = true;
57 |
58 | Value.ValidateSemantics(validator);
59 |
60 | Target.ValidateSemantics(validator);
61 |
62 | }
63 |
64 | public override void ValidateTypes(SemanticsValidator validator)
65 | {
66 |
67 | }
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/Nova.Bytecode/IO/ByteClass.cs:
--------------------------------------------------------------------------------
1 | using Nova.Bytecode.Enums;
2 | using Nova.Bytecode.Symbols;
3 | using Nova.Utils.IO;
4 | using System;
5 | using System.Collections.Generic;
6 | using System.IO;
7 | using System.Linq;
8 | using System.Text;
9 | using System.Threading.Tasks;
10 |
11 | namespace Nova.ByteCode.IO
12 | {
13 | public class ByteClass : IByteElement
14 | {
15 | public string Name
16 | {
17 | get;
18 | private set;
19 | }
20 | public ContainerType Type
21 | {
22 | get;
23 | private set;
24 | }
25 | public List Methods
26 | {
27 | get;
28 | set;
29 | }
30 |
31 | public List Fields
32 | {
33 | get;
34 | set;
35 | }
36 |
37 | public NovFile NovFile
38 | {
39 | get;
40 | private set;
41 | }
42 |
43 | public ByteClass(NovFile file, string name, ContainerType type)
44 | {
45 | this.NovFile = file;
46 | this.Name = name;
47 | this.Type = type;
48 | this.Methods = new List();
49 | this.Fields = new List();
50 | }
51 |
52 |
53 | public void Serialize(CppBinaryWriter writer)
54 | {
55 | writer.Write(Name);
56 |
57 | writer.Write((byte)Type);
58 |
59 | writer.Write(Methods.Count);
60 |
61 | foreach (var method in Methods)
62 | {
63 | method.Serialize(writer);
64 | }
65 |
66 | writer.Write(Fields.Count);
67 |
68 | foreach (var field in Fields)
69 | {
70 | field.Serialize(writer);
71 | }
72 | }
73 | }
74 | }
75 |
--------------------------------------------------------------------------------
/Nova/Statements/VectorCreationStatement.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Text.RegularExpressions;
6 | using System.Threading.Tasks;
7 | using Antlr4.Runtime;
8 | using Nova.Bytecode.Codes;
9 | using Nova.ByteCode.Generation;
10 | using Nova.IO;
11 | using Nova.Lexer;
12 | using Nova.Members;
13 | using Nova.Semantics;
14 |
15 | namespace Nova.Statements
16 | {
17 | public class VectorCreationStatement : Statement
18 | {
19 | private List Elements
20 | {
21 | get;
22 | set;
23 | }
24 |
25 | public VectorCreationStatement(IChild parent, List elements, ParserRuleContext context) : base(parent, context)
26 | {
27 | this.Elements = elements;
28 | }
29 |
30 | public override void GenerateBytecode(ClassesContainer container, ByteBlock context)
31 | {
32 | Class targetClass = container.TryGetClass(Constants.STDVectorClassName);
33 |
34 | context.Instructions.Add(new StructCreateCode(container.GetClassId(Constants.STDVectorClassName)));
35 |
36 | foreach (var element in Elements)
37 | {
38 | element.GenerateBytecode(container, context);
39 | }
40 |
41 | context.Instructions.Add(new VectCreateCode(Elements.Count));
42 |
43 | context.Instructions.Add(new CtorCallCode(targetClass.GetCtor().Id, 1));
44 | }
45 |
46 | public override void ValidateSemantics(SemanticsValidator validator)
47 | {
48 | foreach (var element in Elements)
49 | {
50 | element.ValidateSemantics(validator);
51 | }
52 | }
53 |
54 | public override void ValidateTypes(SemanticsValidator validator)
55 | {
56 |
57 | }
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/Nova.Bytecode/IO/ByteMethod.cs:
--------------------------------------------------------------------------------
1 | using Nova.ByteCode.Codes;
2 | using Nova.ByteCode.Enums;
3 | using Nova.ByteCode.Generation;
4 | using Nova.Utils;
5 | using Nova.Utils.IO;
6 | using System;
7 | using System.Collections.Generic;
8 | using System.IO;
9 | using System.Linq;
10 | using System.Text;
11 | using System.Threading.Tasks;
12 |
13 | namespace Nova.ByteCode.IO
14 | {
15 | public class ByteMethod : IByteElement
16 | {
17 | public string Name
18 | {
19 | get;
20 | set;
21 | }
22 | public ByteBlock ByteBlock
23 | {
24 | get;
25 | set;
26 | }
27 | public ByteClass ParentClass
28 | {
29 | get;
30 | set;
31 | }
32 | public ModifiersEnum Modifiers
33 | {
34 | get;
35 | set;
36 | }
37 | public int ParametersCount
38 | {
39 | get;
40 | set;
41 | }
42 | public ByteMethod(string name, ModifiersEnum modifiers, int parametersCount, ByteClass parentClass)
43 | {
44 | this.Name = name;
45 | this.Modifiers = modifiers;
46 | this.ByteBlock = new ByteBlock(parentClass);
47 | this.ParentClass = parentClass;
48 | this.ParametersCount = parametersCount;
49 | }
50 | public void Print()
51 | {
52 | Logger.Write("-------" + ToString() + " bytecode--------", LogType.Color2);
53 | ByteBlock.Print();
54 | Logger.Write("-------" + ToString() + " bytecode--------", LogType.Color2);
55 | }
56 | public override string ToString()
57 | {
58 | return Name + "()";
59 | }
60 | public void Serialize(CppBinaryWriter writer)
61 | {
62 | writer.Write(Name);
63 | writer.Write((byte)Modifiers);
64 | writer.Write(ParametersCount);
65 | ByteBlock.Serialize(writer);
66 | }
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/Nova/Expressions/NativeCallExpression.cs:
--------------------------------------------------------------------------------
1 | using Antlr4.Runtime;
2 | using Nova.Bytecode.Enums;
3 | using Nova.ByteCode.Codes;
4 | using Nova.ByteCode.Generation;
5 | using Nova.IO;
6 | using Nova.Lexer;
7 | using Nova.Members;
8 | using Nova.Semantics;
9 | using Nova.Statements;
10 | using System;
11 | using System.Collections.Generic;
12 | using System.Linq;
13 | using System.Text;
14 | using System.Threading.Tasks;
15 |
16 | namespace Nova.Expressions
17 | {
18 | public class NativeCallExpression : Expression
19 | {
20 | private string NativeName
21 | {
22 | get;
23 | set;
24 | }
25 | public List Parameters
26 | {
27 | get;
28 | set;
29 | }
30 | private NativesEnum NativeEnum
31 | {
32 | get;
33 | set;
34 | }
35 | public NativeCallExpression(IChild parent, string nativeName, ParserRuleContext context) : base(parent, context)
36 | {
37 | this.NativeName = nativeName;
38 | this.NativeName = nativeName;
39 | }
40 |
41 | public override void GenerateBytecode(ClassesContainer container, ByteBlock context)
42 | {
43 | foreach (var parameter in Parameters)
44 | {
45 | parameter.GenerateBytecode(container, context);
46 | }
47 |
48 | context.Instructions.Add(new NativeCallCode((int)NativeEnum));
49 |
50 | }
51 |
52 | public override void ValidateSemantics(SemanticsValidator validator)
53 | {
54 | NativesEnum result = NativesEnum.Unknown;
55 |
56 | if (!Enum.TryParse(NativeName, out result) || result == NativesEnum.Unknown)
57 | {
58 | validator.AddError("Unknown native function : " + NativeName, ParsingContext);
59 | }
60 |
61 | NativeEnum = result;
62 | foreach (var parameter in Parameters)
63 | {
64 | parameter.ValidateSemantics(validator);
65 | }
66 | }
67 |
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/Nova/Statements/WhileStatement.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.IO;
4 | using System.Linq;
5 | using System.Text;
6 | using System.Text.RegularExpressions;
7 | using System.Threading.Tasks;
8 | using Nova.Bytecode.Codes;
9 | using Nova.ByteCode.Codes;
10 | using Nova.ByteCode.Generation;
11 | using Nova.Lexer;
12 | using Nova.IO;
13 | using Nova.Members;
14 | using Nova.Semantics;
15 | using Antlr4.Runtime;
16 |
17 | namespace Nova.Statements
18 | {
19 | public class WhileStatement : Statement
20 | {
21 | public ExpressionNode Condition
22 | {
23 | get;
24 | set;
25 | }
26 | public List Statements
27 | {
28 | get;
29 | set;
30 | }
31 |
32 | public WhileStatement(IChild parent, ParserRuleContext context) : base(parent, context)
33 | {
34 |
35 |
36 |
37 | }
38 |
39 |
40 | public override void GenerateBytecode(ClassesContainer container, ByteBlock context)
41 | {
42 | int jumpIndex = context.NextOpIndex;
43 |
44 | Condition.GenerateBytecode(container, context);
45 |
46 | JumpIfFalseCode jumpIfFalse = new JumpIfFalseCode(-1);
47 |
48 | context.Instructions.Add(jumpIfFalse);
49 |
50 | foreach (var statement in Statements)
51 | {
52 | statement.GenerateBytecode(container, context);
53 | }
54 |
55 | context.Instructions.Add(new JumpCode(jumpIndex));
56 |
57 | jumpIfFalse.targetIndex = context.NextOpIndex;
58 |
59 | }
60 |
61 | public override void ValidateSemantics(SemanticsValidator validator)
62 | {
63 | Condition.ValidateSemantics(validator);
64 |
65 | validator.BlockStart();
66 |
67 | foreach (var statement in Statements)
68 | {
69 | statement.ValidateSemantics(validator);
70 | }
71 |
72 | validator.BlockEnd();
73 | }
74 |
75 | public override void ValidateTypes(SemanticsValidator validator)
76 | {
77 |
78 | }
79 | }
80 | }
81 |
--------------------------------------------------------------------------------
/Nova.Compiler/Program.cs:
--------------------------------------------------------------------------------
1 | using Nova.ByteCode;
2 | using Nova.ByteCode.Generation;
3 | using Nova.IO;
4 | using Nova.Lexer;
5 | using Nova.Utils;
6 | using Nova.Utils.IO;
7 | using System;
8 | using System.Collections.Generic;
9 | using System.Diagnostics;
10 | using System.IO;
11 | using System.Linq;
12 | using System.Reflection;
13 | using System.Text;
14 | using System.Text.RegularExpressions;
15 | using System.Threading.Tasks;
16 |
17 |
18 | namespace Nova.Compiler
19 | {
20 | class Program
21 | {
22 | static void Main(string[] args)
23 | {
24 | Console.Title = Assembly.GetCallingAssembly().GetName().Name;
25 |
26 | if (args.Length == 0)
27 | {
28 | Logger.Write("You need to specify a nova file (.nv).", LogType.Warning);
29 | Console.Read();
30 | return;
31 | }
32 | if (args.Length > 2)
33 | {
34 | Logger.Write("Args are [scriptPath] [outputPath]?");
35 | Console.Read();
36 | return;
37 | }
38 |
39 | bool outputPathSpecified = args.Length == 2;
40 |
41 | string outputPath;
42 |
43 | if (outputPathSpecified)
44 | {
45 | outputPath = args[1];
46 | Logger.Write("Output path specified : " + outputPath, LogType.Debug);
47 |
48 | }
49 | else
50 | {
51 | Logger.Write("Using default ouput path : " + Constants.DefaultOuputPath, LogType.Debug);
52 | outputPath = Constants.DefaultOuputPath;
53 | }
54 |
55 | Stopwatch st = Stopwatch.StartNew();
56 |
57 | NovBuilder builder = new NovBuilder(args[0], outputPath);
58 |
59 | if (!builder.Build())
60 | {
61 | Console.Read();
62 | return;
63 | }
64 |
65 | builder.Save();
66 |
67 | Logger.Write(outputPath + " generated in " + st.ElapsedMilliseconds + "ms", LogType.Debug);
68 |
69 | builder.PrintMainByteCode();
70 |
71 | Console.Read();
72 |
73 | }
74 | }
75 | }
76 |
--------------------------------------------------------------------------------
/Nova.Bytecode/Generation/ByteBlock.cs:
--------------------------------------------------------------------------------
1 | using Nova.Bytecode.Symbols;
2 | using Nova.ByteCode.Codes;
3 | using Nova.ByteCode.IO;
4 | using Nova.Utils;
5 | using Nova.Utils.IO;
6 | using System;
7 | using System.Collections.Generic;
8 | using System.IO;
9 | using System.Linq;
10 | using System.Text;
11 | using System.Threading.Tasks;
12 |
13 | namespace Nova.ByteCode.Generation
14 | {
15 | public class ByteBlock : IByteElement
16 | {
17 | public SymbolTable SymbolTable
18 | {
19 | get;
20 | private set;
21 | }
22 | public List Instructions
23 | {
24 | get;
25 | set;
26 | }
27 | public int NextOpIndex
28 | {
29 | get
30 | {
31 | return Instructions.Sum(x => x.GetSize() + 1);
32 | }
33 | }
34 | public int LocalsCount
35 | {
36 | get
37 | {
38 | return SymbolTable.Count;
39 | }
40 | }
41 | public ByteClass ParentClass
42 | {
43 | get;
44 | private set;
45 | }
46 | public ByteBlock(ByteClass parentClass)
47 | {
48 | this.SymbolTable = new SymbolTable();
49 | this.Instructions = new List();
50 | this.ParentClass = parentClass;
51 | }
52 |
53 | public void Print()
54 | {
55 | foreach (var byteCode in Instructions)
56 | {
57 | Logger.Write(byteCode);
58 | }
59 | }
60 |
61 | public int BindConstant(object value)
62 | {
63 | return ParentClass.NovFile.BindConstant(value);
64 | }
65 |
66 | public void Serialize(CppBinaryWriter writer)
67 | {
68 | //SymbolTable.Serialize(writer);
69 |
70 | int size = Instructions.Sum(x => x.GetSize() + 1);
71 |
72 | writer.Write(size);
73 |
74 | foreach (var code in Instructions)
75 | {
76 | writer.Write(code.OpId);
77 | code.Serialize(writer);
78 | }
79 |
80 | writer.Write(LocalsCount);
81 | }
82 |
83 | }
84 | }
85 |
--------------------------------------------------------------------------------
/Nova/Parser/Listeners/ClassListener.cs:
--------------------------------------------------------------------------------
1 | using Antlr4.Runtime.Misc;
2 | using Antlr4.Runtime.Tree;
3 | using Nova.Bytecode.Enums;
4 | using Nova.IO;
5 | using Nova.Members;
6 | using System;
7 | using static NovaParser;
8 | using IErrorNode = Antlr4.Runtime.Tree.IErrorNode;
9 | using ITerminalNode = Antlr4.Runtime.Tree.ITerminalNode;
10 | using IToken = Antlr4.Runtime.IToken;
11 | using ParserRuleContext = Antlr4.Runtime.ParserRuleContext;
12 |
13 | namespace Nova.Parser.Listeners
14 | {
15 | public partial class ClassListener : NovaParserBaseListener
16 | {
17 | private NvFile File
18 | {
19 | get;
20 | set;
21 | }
22 | public ClassListener(NvFile file)
23 | {
24 | this.File = file;
25 | }
26 |
27 | public override void EnterTypeDeclaration([NotNull] TypeDeclarationContext context)
28 | {
29 | foreach (var rule in context.GetRuleContexts())
30 | {
31 | rule.EnterRule(this);
32 | }
33 | }
34 | public override void EnterPrimitiveDeclaration([NotNull] PrimitiveDeclarationContext context)
35 | {
36 | string name = context.primitiveTypeOrUnit().GetText();
37 | AddMember(name, ContainerType.primitive, context);
38 | }
39 | public override void EnterClassDeclaration(NovaParser.ClassDeclarationContext context)
40 | {
41 | string name = context.IDENTIFIER().GetText();
42 | AddMember(name, ContainerType.@class, context);
43 | }
44 | public override void EnterStructDeclaration([NotNull] NovaParser.StructDeclarationContext context)
45 | {
46 | AddMember(context.IDENTIFIER().GetText(), ContainerType.@struct, context);
47 | }
48 |
49 | private void AddMember(string className, ContainerType type, ParserRuleContext context)
50 | {
51 | Class @class = new Class(File, className, type, context);
52 | ClassMemberListener listener = new ClassMemberListener(@class);
53 |
54 | foreach (var memberDeclaration in context.GetRuleContext(0).GetRuleContexts())
55 | {
56 | memberDeclaration.EnterRule(listener);
57 | }
58 |
59 | File.Classes.Add(@class);
60 | }
61 |
62 |
63 | }
64 | }
--------------------------------------------------------------------------------
/Nova.Utils/Logger.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace Nova.Utils
8 | {
9 | public enum LogType
10 | {
11 | None = -1,
12 | Log = 0,
13 | Debug = 1,
14 | SemanticError = 2,
15 | SyntaxicError = 3,
16 | Error = 5,
17 | LogImportant = 6,
18 | Success = 7,
19 | Warning = 8,
20 | ProgramOutput = 9,
21 | Color1,
22 | Color2,
23 | }
24 | public class Logger
25 | {
26 | private const ConsoleColor COLOR_1 = ConsoleColor.Magenta;
27 | private const ConsoleColor COLOR_2 = ConsoleColor.DarkMagenta;
28 |
29 | private static LogType[] NoPrefix = new LogType[]
30 | {
31 | LogType.None,
32 | LogType.Color1,
33 | LogType.Color2,
34 | };
35 | private static Dictionary Colors = new Dictionary()
36 | {
37 | { LogType.None,ConsoleColor.Gray },
38 | { LogType.Log, ConsoleColor.Gray },
39 | { LogType.Debug, ConsoleColor.DarkGray },
40 | { LogType.LogImportant, ConsoleColor.White },
41 |
42 | { LogType.Error, ConsoleColor.Red },
43 | { LogType.SemanticError, ConsoleColor.Red },
44 | { LogType.SyntaxicError, ConsoleColor.Red },
45 |
46 | { LogType.Warning, ConsoleColor.Yellow},
47 |
48 | { LogType.Success, ConsoleColor.Green },
49 | { LogType.ProgramOutput, ConsoleColor.Magenta },
50 |
51 | { LogType.Color1, ConsoleColor.DarkMagenta },
52 | { LogType.Color2, ConsoleColor.DarkMagenta }
53 | };
54 |
55 | public static void Write(object value, LogType state = LogType.None)
56 | {
57 | if (!NoPrefix.Contains(state))
58 | {
59 | Console.ForegroundColor = ConsoleColor.Gray;
60 | Console.Write(state + " > ");
61 | }
62 |
63 | WriteColored(value, Colors[state]);
64 | }
65 | private static void WriteColored(object value, ConsoleColor color)
66 | {
67 | Console.ForegroundColor = color;
68 | Console.WriteLine(value);
69 | }
70 | public static void NewLine()
71 | {
72 | Console.WriteLine(Environment.NewLine);
73 | }
74 | }
75 | }
76 |
--------------------------------------------------------------------------------
/Nova/Statements/ForStatement.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Text.RegularExpressions;
6 | using System.Threading.Tasks;
7 | using Antlr4.Runtime;
8 | using Nova.ByteCode.Codes;
9 | using Nova.ByteCode.Generation;
10 | using Nova.IO;
11 | using Nova.Lexer;
12 | using Nova.Members;
13 | using Nova.Semantics;
14 |
15 | namespace Nova.Statements
16 | {
17 | public class ForStatement : Statement
18 | {
19 | public Statement Init
20 | {
21 | get;
22 | set;
23 | }
24 | public ExpressionNode Condition
25 | {
26 | get;
27 | set;
28 | }
29 | public Statement Update
30 | {
31 | get;
32 | set;
33 | }
34 | public List Statements
35 | {
36 | get;
37 | set;
38 | }
39 |
40 | public ForStatement(IChild parent, ParserRuleContext context) : base(parent, context)
41 | {
42 |
43 | }
44 |
45 | public override void GenerateBytecode(ClassesContainer container, ByteBlock context)
46 | {
47 | Init.GenerateBytecode(container, context);
48 |
49 | int jumpIndex = context.NextOpIndex;
50 |
51 | Condition.GenerateBytecode(container, context);
52 |
53 | JumpIfFalseCode code = new JumpIfFalseCode(-1);
54 | context.Instructions.Add(code);
55 |
56 | foreach (var statement in this.Statements)
57 | {
58 | statement.GenerateBytecode(container, context);
59 | }
60 |
61 | Update.GenerateBytecode(container, context);
62 |
63 | context.Instructions.Add(new JumpCode(jumpIndex));
64 | code.targetIndex = context.NextOpIndex;
65 |
66 |
67 |
68 |
69 | }
70 |
71 | public override void ValidateSemantics(SemanticsValidator validator)
72 | {
73 | validator.BlockStart();
74 |
75 | Init.ValidateSemantics(validator);
76 | Condition.ValidateSemantics(validator);
77 | Update.ValidateSemantics(validator);
78 |
79 | foreach (var statement in Statements)
80 | {
81 | statement.ValidateSemantics(validator);
82 | }
83 |
84 | validator.BlockEnd();
85 | }
86 |
87 | public override void ValidateTypes(SemanticsValidator validator)
88 | {
89 |
90 | }
91 | }
92 | }
93 |
--------------------------------------------------------------------------------
/Nova.Bytecode/IO/NovFile.cs:
--------------------------------------------------------------------------------
1 | using Nova.Bytecode.IO;
2 | using Nova.Utils;
3 | using Nova.Utils.IO;
4 | using System;
5 | using System.Collections.Generic;
6 | using System.IO;
7 | using System.Linq;
8 | using System.Text;
9 | using System.Threading.Tasks;
10 |
11 | namespace Nova.ByteCode.IO
12 | {
13 | public class NovFile : IByteElement
14 | {
15 | public const string HEADER = "NovaEX";
16 |
17 | public List ByteClasses
18 | {
19 | get;
20 | private set;
21 | }
22 | public MainPointEntry MainPointEntry
23 | {
24 | get;
25 | set;
26 | }
27 | private List