-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
drimerdev
committed
Feb 26, 2024
1 parent
eaf8468
commit 6d16cdd
Showing
4 changed files
with
193 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0-windows10.0.22000.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<StartupObject>MigScriptInterpreter.Program</StartupObject> | ||
<ApplicationIcon>MS.ico</ApplicationIcon> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Content Include="MS.ico" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 17 | ||
VisualStudioVersion = 17.9.34616.47 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MigScript", "MigScript.csproj", "{56D98B87-AE0F-4BCD-A9A6-D0C6A74AD8DE}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{56D98B87-AE0F-4BCD-A9A6-D0C6A74AD8DE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{56D98B87-AE0F-4BCD-A9A6-D0C6A74AD8DE}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{56D98B87-AE0F-4BCD-A9A6-D0C6A74AD8DE}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{56D98B87-AE0F-4BCD-A9A6-D0C6A74AD8DE}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {399C9404-49FE-4EB9-AFC8-FB4087E8ABCE} | ||
EndGlobalSection | ||
EndGlobal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace MigScriptInterpreter | ||
{ | ||
public enum TokenType | ||
{ | ||
Identifier, | ||
Number, | ||
String, | ||
Keyword, | ||
Symbol | ||
// Add more token types as needed | ||
} | ||
|
||
public struct Token | ||
{ | ||
public TokenType Type { get; } | ||
public string Lexeme { get; } | ||
public int LineNumber { get; } | ||
|
||
public Token(TokenType type, string lexeme, int lineNumber) | ||
{ | ||
Type = type; | ||
Lexeme = lexeme; | ||
LineNumber = lineNumber; | ||
} | ||
} | ||
|
||
public class Lexer | ||
{ | ||
private readonly string _source; | ||
private int _currentPosition; | ||
private int _currentLineNumber; | ||
|
||
public Lexer(string source) | ||
{ | ||
_source = source; | ||
_currentPosition = 0; | ||
_currentLineNumber = 1; | ||
} | ||
|
||
private bool IsAtEnd() => _currentPosition >= _source.Length; | ||
|
||
private char Advance() => _source[_currentPosition++]; | ||
|
||
private char Peek() => IsAtEnd() ? '\0' : _source[_currentPosition]; | ||
|
||
private void SkipWhitespace() | ||
{ | ||
while (!IsAtEnd() && char.IsWhiteSpace(Peek())) | ||
{ | ||
if (Peek() == '\n') _currentLineNumber++; | ||
_ = Advance(); | ||
} | ||
} | ||
|
||
private Token LexNextToken() | ||
{ | ||
SkipWhitespace(); | ||
if (IsAtEnd()) return new Token(TokenType.Symbol, "", _currentLineNumber); | ||
|
||
char c = Advance(); | ||
if (char.IsLetter(c)) | ||
{ | ||
// Identifier or keyword | ||
string lexeme = c.ToString(); | ||
while (char.IsLetterOrDigit(Peek()) || Peek() == '_') | ||
{ | ||
lexeme += Advance(); | ||
} | ||
// Check if it's a keyword | ||
if (lexeme == "let" || lexeme == "writl") | ||
{ | ||
return new Token(TokenType.Keyword, lexeme, _currentLineNumber); | ||
} | ||
return new Token(TokenType.Identifier, lexeme, _currentLineNumber); | ||
} | ||
else if (char.IsDigit(c)) | ||
{ | ||
// Number | ||
string lexeme = c.ToString(); | ||
while (char.IsDigit(Peek())) | ||
{ | ||
lexeme += Advance(); | ||
} | ||
return new Token(TokenType.Number, lexeme, _currentLineNumber); | ||
} | ||
else if (c == '"') | ||
{ | ||
// String | ||
string lexeme = ""; | ||
while (Peek() != '"' && !IsAtEnd()) | ||
{ | ||
if (Peek() == '\n') _currentLineNumber++; | ||
lexeme += Advance(); | ||
} | ||
if (Peek() == '"') _ = Advance(); // Consume the closing quote | ||
return new Token(TokenType.String, lexeme, _currentLineNumber); | ||
} | ||
else | ||
{ | ||
// Symbol | ||
return new Token(TokenType.Symbol, c.ToString(), _currentLineNumber); | ||
} | ||
} | ||
|
||
public List<Token> Tokenize() | ||
{ | ||
List<Token> tokens = new(); | ||
while (!IsAtEnd()) | ||
{ | ||
tokens.Add(LexNextToken()); | ||
} | ||
return tokens; | ||
} | ||
} | ||
|
||
public class Interpreter | ||
{ | ||
public void Interpret(List<Token> tokens) | ||
{ | ||
foreach (var token in tokens) | ||
{ | ||
Console.WriteLine($"Token: {token.Lexeme} | Type: {token.Type} | Line: {token.LineNumber}"); | ||
} | ||
} | ||
} | ||
|
||
class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
Console.WriteLine("MigScript Interpreter CLI"); | ||
Console.WriteLine("Enter MigScript code (press Enter twice to finish):"); | ||
|
||
string source = ""; | ||
string line; | ||
while ((line = Console.ReadLine()) != "") | ||
{ | ||
source += line + "\n"; | ||
} | ||
|
||
Lexer lexer = new(source); | ||
List<Token> tokens = lexer.Tokenize(); | ||
|
||
Interpreter interpreter = new(); | ||
interpreter.Interpret(tokens); | ||
} | ||
} | ||
} |