Compare commits
2 Commits
1d8d02f2a8
...
V0.0.2-bet
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1cf89efb4b | ||
|
|
1884950f6f |
7
PCCcompiler.slnx
Normal file
7
PCCcompiler.slnx
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
<Solution>
|
||||||
|
<Configurations>
|
||||||
|
<Platform Name="x64" />
|
||||||
|
<Platform Name="x86" />
|
||||||
|
</Configurations>
|
||||||
|
<Project Path="PCCcompiler/PCCcompiler.vcxproj" Id="60a99674-9bdb-4552-bfef-fc5b17de2cf8" />
|
||||||
|
</Solution>
|
||||||
144
PCCcompiler/PCCcompiler.vcxproj
Normal file
144
PCCcompiler/PCCcompiler.vcxproj
Normal file
@@ -0,0 +1,144 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||||
|
<ItemGroup Label="ProjectConfigurations">
|
||||||
|
<ProjectConfiguration Include="Debug|Win32">
|
||||||
|
<Configuration>Debug</Configuration>
|
||||||
|
<Platform>Win32</Platform>
|
||||||
|
</ProjectConfiguration>
|
||||||
|
<ProjectConfiguration Include="Release|Win32">
|
||||||
|
<Configuration>Release</Configuration>
|
||||||
|
<Platform>Win32</Platform>
|
||||||
|
</ProjectConfiguration>
|
||||||
|
<ProjectConfiguration Include="Debug|x64">
|
||||||
|
<Configuration>Debug</Configuration>
|
||||||
|
<Platform>x64</Platform>
|
||||||
|
</ProjectConfiguration>
|
||||||
|
<ProjectConfiguration Include="Release|x64">
|
||||||
|
<Configuration>Release</Configuration>
|
||||||
|
<Platform>x64</Platform>
|
||||||
|
</ProjectConfiguration>
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<ClCompile Include="codegen.cpp" />
|
||||||
|
<ClCompile Include="main.cpp" />
|
||||||
|
<ClCompile Include="parser.cpp" />
|
||||||
|
<ClCompile Include="utils.cpp" />
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<ClInclude Include="codegen.h" />
|
||||||
|
<ClInclude Include="compiler_types.h" />
|
||||||
|
<ClInclude Include="parser.h" />
|
||||||
|
<ClInclude Include="utils.h" />
|
||||||
|
</ItemGroup>
|
||||||
|
<PropertyGroup Label="Globals">
|
||||||
|
<VCProjectVersion>18.0</VCProjectVersion>
|
||||||
|
<Keyword>Win32Proj</Keyword>
|
||||||
|
<ProjectGuid>{60a99674-9bdb-4552-bfef-fc5b17de2cf8}</ProjectGuid>
|
||||||
|
<RootNamespace>PCCcompiler</RootNamespace>
|
||||||
|
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
|
||||||
|
</PropertyGroup>
|
||||||
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||||
|
<ConfigurationType>Application</ConfigurationType>
|
||||||
|
<UseDebugLibraries>true</UseDebugLibraries>
|
||||||
|
<PlatformToolset>v145</PlatformToolset>
|
||||||
|
<CharacterSet>Unicode</CharacterSet>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||||
|
<ConfigurationType>Application</ConfigurationType>
|
||||||
|
<UseDebugLibraries>false</UseDebugLibraries>
|
||||||
|
<PlatformToolset>v145</PlatformToolset>
|
||||||
|
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||||
|
<CharacterSet>Unicode</CharacterSet>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
|
||||||
|
<ConfigurationType>Application</ConfigurationType>
|
||||||
|
<UseDebugLibraries>true</UseDebugLibraries>
|
||||||
|
<PlatformToolset>v145</PlatformToolset>
|
||||||
|
<CharacterSet>Unicode</CharacterSet>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
|
||||||
|
<ConfigurationType>Application</ConfigurationType>
|
||||||
|
<UseDebugLibraries>false</UseDebugLibraries>
|
||||||
|
<PlatformToolset>v145</PlatformToolset>
|
||||||
|
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||||
|
<CharacterSet>Unicode</CharacterSet>
|
||||||
|
</PropertyGroup>
|
||||||
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||||
|
<ImportGroup Label="ExtensionSettings">
|
||||||
|
</ImportGroup>
|
||||||
|
<ImportGroup Label="Shared">
|
||||||
|
</ImportGroup>
|
||||||
|
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||||
|
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||||
|
</ImportGroup>
|
||||||
|
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||||
|
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||||
|
</ImportGroup>
|
||||||
|
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||||
|
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||||
|
</ImportGroup>
|
||||||
|
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||||
|
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||||
|
</ImportGroup>
|
||||||
|
<PropertyGroup Label="UserMacros" />
|
||||||
|
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||||
|
<ClCompile>
|
||||||
|
<WarningLevel>Level3</WarningLevel>
|
||||||
|
<SDLCheck>true</SDLCheck>
|
||||||
|
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||||
|
<ConformanceMode>true</ConformanceMode>
|
||||||
|
<LanguageStandard>stdcpp20</LanguageStandard>
|
||||||
|
</ClCompile>
|
||||||
|
<Link>
|
||||||
|
<SubSystem>Console</SubSystem>
|
||||||
|
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||||
|
</Link>
|
||||||
|
</ItemDefinitionGroup>
|
||||||
|
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||||
|
<ClCompile>
|
||||||
|
<WarningLevel>Level3</WarningLevel>
|
||||||
|
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||||
|
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||||
|
<SDLCheck>true</SDLCheck>
|
||||||
|
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||||
|
<ConformanceMode>true</ConformanceMode>
|
||||||
|
<LanguageStandard>stdcpp20</LanguageStandard>
|
||||||
|
</ClCompile>
|
||||||
|
<Link>
|
||||||
|
<SubSystem>Console</SubSystem>
|
||||||
|
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||||
|
</Link>
|
||||||
|
</ItemDefinitionGroup>
|
||||||
|
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||||
|
<ClCompile>
|
||||||
|
<WarningLevel>Level3</WarningLevel>
|
||||||
|
<SDLCheck>true</SDLCheck>
|
||||||
|
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||||
|
<ConformanceMode>true</ConformanceMode>
|
||||||
|
<LanguageStandard>stdcpp20</LanguageStandard>
|
||||||
|
</ClCompile>
|
||||||
|
<Link>
|
||||||
|
<SubSystem>Console</SubSystem>
|
||||||
|
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||||
|
</Link>
|
||||||
|
</ItemDefinitionGroup>
|
||||||
|
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||||
|
<ClCompile>
|
||||||
|
<WarningLevel>Level3</WarningLevel>
|
||||||
|
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||||
|
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||||
|
<SDLCheck>true</SDLCheck>
|
||||||
|
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||||
|
<ConformanceMode>true</ConformanceMode>
|
||||||
|
<LanguageStandard>stdcpp20</LanguageStandard>
|
||||||
|
</ClCompile>
|
||||||
|
<Link>
|
||||||
|
<SubSystem>Console</SubSystem>
|
||||||
|
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||||
|
</Link>
|
||||||
|
</ItemDefinitionGroup>
|
||||||
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||||
|
<ImportGroup Label="ExtensionTargets">
|
||||||
|
</ImportGroup>
|
||||||
|
</Project>
|
||||||
45
PCCcompiler/PCCcompiler.vcxproj.filters
Normal file
45
PCCcompiler/PCCcompiler.vcxproj.filters
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||||
|
<ItemGroup>
|
||||||
|
<Filter Include="Pliki źródłowe">
|
||||||
|
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
|
||||||
|
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
|
||||||
|
</Filter>
|
||||||
|
<Filter Include="Pliki nagłówkowe">
|
||||||
|
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
|
||||||
|
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
|
||||||
|
</Filter>
|
||||||
|
<Filter Include="Pliki zasobów">
|
||||||
|
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
|
||||||
|
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
|
||||||
|
</Filter>
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<ClCompile Include="main.cpp">
|
||||||
|
<Filter>Pliki źródłowe</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="utils.cpp">
|
||||||
|
<Filter>Pliki źródłowe</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="parser.cpp">
|
||||||
|
<Filter>Pliki źródłowe</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="codegen.cpp">
|
||||||
|
<Filter>Pliki źródłowe</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<ClInclude Include="utils.h">
|
||||||
|
<Filter>Pliki nagłówkowe</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="parser.h">
|
||||||
|
<Filter>Pliki nagłówkowe</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="codegen.h">
|
||||||
|
<Filter>Pliki nagłówkowe</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="compiler_types.h">
|
||||||
|
<Filter>Pliki nagłówkowe</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
</ItemGroup>
|
||||||
|
</Project>
|
||||||
81
PCCcompiler/codegen.cpp
Normal file
81
PCCcompiler/codegen.cpp
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
#include "codegen.h"
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
std::string generateAssembly(const CompilerState& state) {
|
||||||
|
std::string result;
|
||||||
|
|
||||||
|
// --- NAG£ÓWEK I SEKCJA DATA ---
|
||||||
|
result += "global main\n";
|
||||||
|
result += "extern printf\n";
|
||||||
|
result += "extern GetAsyncKeyState\n\n";
|
||||||
|
|
||||||
|
result += "section .data\n";
|
||||||
|
|
||||||
|
// Generowanie zmiennych
|
||||||
|
for (const auto& v : state.variables) {
|
||||||
|
result += " " + v.first + " dd " + std::to_string(v.second) + "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sta³e stringi
|
||||||
|
result += " fmt db '%d', 10, 0\n";
|
||||||
|
result += " pause_msg db 'Nacisnij ESC aby zamknac...', 10, 0\n\n";
|
||||||
|
|
||||||
|
result += "section .text\n";
|
||||||
|
|
||||||
|
// --- DEFINICJE FUNKCJI U¯YTKOWNIKA ---
|
||||||
|
for (const auto& func : state.functions) {
|
||||||
|
result += func.first + ":\n";
|
||||||
|
result += " sub rsp, 40\n"; // Shadow space
|
||||||
|
|
||||||
|
// Printy wewn¹trz funkcji
|
||||||
|
for (const std::string& var : func.second) {
|
||||||
|
if (state.variables.count(var)) {
|
||||||
|
result += " mov edx, [" + var + "]\n";
|
||||||
|
result += " lea rcx, [rel fmt]\n";
|
||||||
|
result += " call printf\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
result += " add rsp, 40\n";
|
||||||
|
result += " ret\n\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- FUNKCJA MAIN ---
|
||||||
|
result += "main:\n";
|
||||||
|
result += " sub rsp, 40\n";
|
||||||
|
|
||||||
|
// Globalne printy (poza funkcjami)
|
||||||
|
for (const std::string& var : state.globalPrints) {
|
||||||
|
if (state.variables.count(var)) {
|
||||||
|
result += " mov edx, [" + var + "]\n";
|
||||||
|
result += " lea rcx, [rel fmt]\n";
|
||||||
|
result += " call printf\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wywo³ania funkcji zdefiniowanych wczeœniej
|
||||||
|
for (const std::string& call : state.printCalls) {
|
||||||
|
if (call.find("CALL_") == 0) {
|
||||||
|
std::string funcName = call.substr(5); // Usuñ prefiks "CALL_"
|
||||||
|
if (state.functions.count(funcName)) {
|
||||||
|
result += " call " + funcName + "\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- PAUSE LOOP (Czekanie na ESC) ---
|
||||||
|
result += " lea rcx, [rel pause_msg]\n";
|
||||||
|
result += " call printf\n";
|
||||||
|
result += "pause_loop:\n";
|
||||||
|
result += " mov ecx, 27\n"; // VK_ESCAPE
|
||||||
|
result += " call GetAsyncKeyState\n";
|
||||||
|
result += " test ax, 8000h\n";
|
||||||
|
result += " jz pause_loop\n";
|
||||||
|
|
||||||
|
// Wyjœcie z programu
|
||||||
|
result += " add rsp, 40\n";
|
||||||
|
result += " ret\n";
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
11
PCCcompiler/codegen.h
Normal file
11
PCCcompiler/codegen.h
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
#ifndef CODEGEN_H
|
||||||
|
#define CODEGEN_H
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include "compiler_types.h"
|
||||||
|
|
||||||
|
// G³ówna funkcja generuj¹ca kod ASM na podstawie stanu kompilatora
|
||||||
|
std::string generateAssembly(const CompilerState& state);
|
||||||
|
|
||||||
|
#endif // CODEGEN_H
|
||||||
|
|
||||||
33
PCCcompiler/compiler_types.h
Normal file
33
PCCcompiler/compiler_types.h
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
#ifndef COMPILER_TYPES_H
|
||||||
|
#define COMPILER_TYPES_H
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
#include <map>
|
||||||
|
#include <stack>
|
||||||
|
|
||||||
|
struct Expression {
|
||||||
|
std::string leftVar;
|
||||||
|
std::string op;
|
||||||
|
std::string rightVar;
|
||||||
|
std::string resultVar;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct IfBlock {
|
||||||
|
std::string conditionVar;
|
||||||
|
std::vector<std::string> prints;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct CompilerState {
|
||||||
|
std::vector<Expression> expressions;
|
||||||
|
std::vector<IfBlock> ifBlocks;
|
||||||
|
std::map<std::string, int> variables;
|
||||||
|
std::vector<std::string> printCalls;
|
||||||
|
std::vector<std::string> globalPrints;
|
||||||
|
std::map<std::string, std::vector<std::string>> functions;
|
||||||
|
bool inFunction = false;
|
||||||
|
std::string currentFunction;
|
||||||
|
std::stack<bool> braceStack;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
134
PCCcompiler/main.cpp
Normal file
134
PCCcompiler/main.cpp
Normal file
@@ -0,0 +1,134 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <fstream>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
#include <cstdlib> // do std::system
|
||||||
|
#include "compiler_types.h"
|
||||||
|
#include "parser.h"
|
||||||
|
#include "codegen.h"
|
||||||
|
|
||||||
|
int main(int argc, char* argv[]) {
|
||||||
|
std::string inputFile, outputName;
|
||||||
|
std::string Version = "v1.5.0-modular"; // Zaktualizowałem wersję :)
|
||||||
|
bool showHelp = false, showVersion = false, showCredits = false;
|
||||||
|
|
||||||
|
// --- PARSOWANIE ARGUMENTÓW ---
|
||||||
|
for (int i = 1; i < argc; i++) {
|
||||||
|
std::string arg = argv[i];
|
||||||
|
|
||||||
|
if (arg == "--help" || arg == "-h") {
|
||||||
|
showHelp = true;
|
||||||
|
}
|
||||||
|
else if (arg == "--version" || arg == "-v") {
|
||||||
|
showVersion = true;
|
||||||
|
}
|
||||||
|
else if (arg == "--credits" || arg == "-c") {
|
||||||
|
showCredits = true;
|
||||||
|
}
|
||||||
|
else if (arg.substr(0, 2) == "-o") {
|
||||||
|
if (arg.size() > 2) {
|
||||||
|
outputName = arg.substr(2);
|
||||||
|
}
|
||||||
|
else if (i + 1 < argc) {
|
||||||
|
outputName = argv[++i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
inputFile = arg;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- OBSŁUGA INFORMACYJNA ---
|
||||||
|
if (showVersion) {
|
||||||
|
std::cout << "PCC Compiler " << Version << " (x64, Modular structure)\n";
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (showCredits) {
|
||||||
|
std::cout << "Created by: Michal Lewandowski\n";
|
||||||
|
std::cout << "Contact : slawek.1q2w3e4r@gmail.com\n"; // Twój email z oryginału
|
||||||
|
std::cout << "Docs: https://nodrop.xyz/PCC_docs.html\n";
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (showHelp || inputFile.empty()) {
|
||||||
|
std::cout << "PCC Compiler " << Version << "\n";
|
||||||
|
std::cout << "Usage: PCC [Options] File.pcc\n\n";
|
||||||
|
std::cout << "Options:\n";
|
||||||
|
std::cout << " -h, --help Show Help\n";
|
||||||
|
std::cout << " -v, --version Show Version\n";
|
||||||
|
std::cout << " -o Name Exe Name (output\\Name.exe)\n";
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- WCZYTYWANIE PLIKU ---
|
||||||
|
std::ifstream in(inputFile);
|
||||||
|
if (!in) {
|
||||||
|
std::cerr << "Error: Cannot open file " << inputFile << "\n";
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string src((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
|
||||||
|
in.close();
|
||||||
|
|
||||||
|
// --- LOGIKA KOMPILATORA ---
|
||||||
|
CompilerState state;
|
||||||
|
|
||||||
|
std::cout << "[INFO] Parsing code...\n";
|
||||||
|
// 1. Parsowanie tekstu do struktur
|
||||||
|
processSource(src, state);
|
||||||
|
|
||||||
|
std::cout << "[INFO] Calculating expressions...\n";
|
||||||
|
// 2. Obliczanie matematyki
|
||||||
|
calculateExpressions(state);
|
||||||
|
|
||||||
|
std::cout << "[INFO] Generating Assembly...\n";
|
||||||
|
// 3. Generowanie kodu ASM
|
||||||
|
std::string asmCode = generateAssembly(state);
|
||||||
|
|
||||||
|
|
||||||
|
// --- ZAPIS I KOMPILACJA ZEWNĘTRZNA ---
|
||||||
|
std::system("if not exist output mkdir output");
|
||||||
|
|
||||||
|
std::string baseName = outputName.empty() ?
|
||||||
|
inputFile.substr(0, inputFile.find_last_of(".")) : outputName;
|
||||||
|
|
||||||
|
// Upewnij się, że nazwa nie ma rozszerzenia .exe w środku ścieżki
|
||||||
|
if (outputName.empty()) {
|
||||||
|
if (baseName.find(".exe") == std::string::npos) baseName += ".exe";
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// Jeśli użytkownik podał nazwę bez .exe, dodaj ją
|
||||||
|
if (outputName.find(".exe") == std::string::npos) outputName += ".exe";
|
||||||
|
baseName = outputName;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ścieżki wyjściowe
|
||||||
|
// Uwaga: zakładam proste nazewnictwo, można tu poprawić usuwanie ścieżek z nazwy pliku
|
||||||
|
std::string asmPath = "output\\" + baseName + ".asm";
|
||||||
|
std::string objPath = "output\\" + baseName + ".obj";
|
||||||
|
std::string exePath = "output\\" + baseName;
|
||||||
|
|
||||||
|
// Zapisz ASM
|
||||||
|
std::ofstream asmOut(asmPath);
|
||||||
|
asmOut << asmCode;
|
||||||
|
asmOut.close();
|
||||||
|
std::cout << "ASM saved to: " << asmPath << "\n";
|
||||||
|
|
||||||
|
// Uruchom NASM
|
||||||
|
std::string nasmCmd = "nasm -f win64 \"" + asmPath + "\" -o \"" + objPath + "\"";
|
||||||
|
if (std::system(nasmCmd.c_str()) != 0) {
|
||||||
|
std::cerr << "NASM Error! Check if NASM is in PATH.\n";
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Uruchom GoLink
|
||||||
|
std::string linkCmd = "go-link.exe /console /entry:main \"" + objPath + "\" msvcrt.dll kernel32.dll user32.dll \"" + exePath + "\"";
|
||||||
|
if (std::system(linkCmd.c_str()) != 0) {
|
||||||
|
std::cerr << "GoLink Error! Check if go-link.exe is in PATH.\n";
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << "SUCCESS! Executable created: " << exePath << "\n";
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
183
PCCcompiler/parser.cpp
Normal file
183
PCCcompiler/parser.cpp
Normal file
@@ -0,0 +1,183 @@
|
|||||||
|
#include "parser.h"
|
||||||
|
#include "utils.h"
|
||||||
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
|
#include "compiler_types.h"
|
||||||
|
|
||||||
|
void processSource(const std::string& src, CompilerState& state) {
|
||||||
|
std::istringstream iss(src);
|
||||||
|
std::string line;
|
||||||
|
|
||||||
|
std::cout << "Parsuje kod:\n";
|
||||||
|
|
||||||
|
while (std::getline(iss, line)) {
|
||||||
|
line = trim(line);
|
||||||
|
if (line.empty()) continue;
|
||||||
|
|
||||||
|
// FUNKCJE
|
||||||
|
if (line.length() > 4 && line.substr(0, 4) == "void") {
|
||||||
|
size_t openParen = line.find("(", 4);
|
||||||
|
size_t closeParen = line.find(")", openParen);
|
||||||
|
if (openParen != std::string::npos && closeParen != std::string::npos) {
|
||||||
|
size_t nameStart = 4;
|
||||||
|
while (nameStart < openParen && (line[nameStart] == ' ' || line[nameStart] == '\t')) nameStart++;
|
||||||
|
size_t nameEnd = openParen;
|
||||||
|
while (nameEnd > nameStart && (line[nameEnd - 1] == ' ' || line[nameEnd - 1] == '\t')) nameEnd--;
|
||||||
|
|
||||||
|
// ZMIANA: używamy 'state' zamiast 'compilerState'
|
||||||
|
state.currentFunction = line.substr(nameStart, nameEnd - nameStart);
|
||||||
|
state.functions[state.currentFunction] = std::vector<std::string>();
|
||||||
|
state.inFunction = true;
|
||||||
|
std::cout << " FUNC " << state.currentFunction << "\n";
|
||||||
|
state.braceStack.push(true);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// }
|
||||||
|
else if (line.find("}") != std::string::npos) {
|
||||||
|
if (!state.braceStack.empty()) {
|
||||||
|
state.braceStack.pop();
|
||||||
|
if (!state.braceStack.empty()) {
|
||||||
|
state.inFunction = false;
|
||||||
|
state.currentFunction.clear();
|
||||||
|
std::cout << " END FUNC\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
// INT
|
||||||
|
if (line.length() > 3 && line.substr(0, 3) == "int") {
|
||||||
|
size_t eqPos = line.find("=", 4);
|
||||||
|
if (eqPos != std::string::npos) {
|
||||||
|
size_t semiPos = line.find(";", eqPos);
|
||||||
|
if (semiPos != std::string::npos) {
|
||||||
|
std::string name = trim(line.substr(4, eqPos - 4));
|
||||||
|
std::string rightSide = trim(line.substr(eqPos + 1, semiPos - eqPos - 1));
|
||||||
|
|
||||||
|
std::string op;
|
||||||
|
size_t opPos;
|
||||||
|
|
||||||
|
size_t eqPos2 = rightSide.find("==");
|
||||||
|
if (eqPos2 != std::string::npos && (eqPos2 + 1 < rightSide.length()) && rightSide[eqPos2 + 2] != '=') {
|
||||||
|
op = "==";
|
||||||
|
opPos = eqPos2;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
opPos = rightSide.find('+');
|
||||||
|
if (opPos == std::string::npos) opPos = rightSide.find('-');
|
||||||
|
if (opPos != std::string::npos) op = rightSide.substr(opPos, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (opPos != std::string::npos && !op.empty()) {
|
||||||
|
std::string leftVar = trim(rightSide.substr(0, opPos));
|
||||||
|
std::string rightVar = trim(rightSide.substr(opPos + op.length()));
|
||||||
|
|
||||||
|
// ZMIANA: używamy 'state'
|
||||||
|
Expression expr{ leftVar, op, rightVar, name };
|
||||||
|
state.expressions.push_back(expr);
|
||||||
|
state.variables[name] = 0;
|
||||||
|
std::cout << " " << op << " EXPR " << name << " = " << leftVar << " " << op << " " << rightVar << "\n";
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
try {
|
||||||
|
int value = std::stoi(rightSide);
|
||||||
|
state.variables[name] = value;
|
||||||
|
std::cout << " VAR " << name << " = " << value << "\n";
|
||||||
|
}
|
||||||
|
catch (...) {
|
||||||
|
// Usunięto '❌' żeby uniknąć warningów o kodowaniu (C4566)
|
||||||
|
std::cout << " [X] BLAD: '" << rightSide << "'\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// BOOL
|
||||||
|
else if (!state.inFunction && line.length() > 4 && line.substr(0, 4) == "bool") {
|
||||||
|
size_t eqPos = line.find("=", 5);
|
||||||
|
if (eqPos != std::string::npos) {
|
||||||
|
size_t semiPos = line.find(";", eqPos);
|
||||||
|
if (semiPos != std::string::npos) {
|
||||||
|
std::string nameRaw = line.substr(5, eqPos - 5);
|
||||||
|
size_t nameStart = nameRaw.find_first_not_of(" \t");
|
||||||
|
size_t nameEnd = nameRaw.find_last_not_of(" \t");
|
||||||
|
std::string name = nameRaw.substr(nameStart, nameEnd - nameStart + 1);
|
||||||
|
|
||||||
|
std::string valueRaw = line.substr(eqPos + 1, semiPos - eqPos - 1);
|
||||||
|
size_t valStart = valueRaw.find_first_not_of(" \t");
|
||||||
|
size_t valEnd = valueRaw.find_last_not_of(" \t");
|
||||||
|
std::string valueStr = valueRaw.substr(valStart, valEnd - valStart + 1);
|
||||||
|
|
||||||
|
bool value = (valueStr == "true" || valueStr == "1");
|
||||||
|
state.variables[name] = value ? 1 : 0;
|
||||||
|
std::cout << " BOOL '" << name << "' = " << (value ? "true" : "false") << "\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// IF
|
||||||
|
else if (line.length() > 3 && line.substr(0, 2) == "if") {
|
||||||
|
size_t openParen = line.find("(");
|
||||||
|
size_t closeParen = line.find(")");
|
||||||
|
|
||||||
|
if (openParen != std::string::npos && closeParen > openParen) {
|
||||||
|
std::string condition = trim(line.substr(openParen + 1, closeParen - openParen - 1));
|
||||||
|
|
||||||
|
IfBlock newIf;
|
||||||
|
newIf.conditionVar = condition;
|
||||||
|
state.ifBlocks.push_back(newIf); // ZMIANA: state.ifBlocks
|
||||||
|
|
||||||
|
std::cout << " IF [" << condition << "] { <- blok #" << state.ifBlocks.size() - 1 << "\n";
|
||||||
|
state.braceStack.push(false);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// PRINT
|
||||||
|
else if (line.length() > 5 && line.substr(0, 5) == "print") {
|
||||||
|
size_t openParen = line.find("(", 5);
|
||||||
|
size_t closeParen = line.rfind(")");
|
||||||
|
if (openParen != std::string::npos && closeParen > openParen) {
|
||||||
|
std::string varName = line.substr(openParen + 1, closeParen - openParen - 1);
|
||||||
|
size_t varStart = varName.find_first_not_of(" \t");
|
||||||
|
size_t varEnd = varName.find_last_not_of(" \t");
|
||||||
|
if (varStart != std::string::npos) {
|
||||||
|
varName = varName.substr(varStart, varEnd - varStart + 1);
|
||||||
|
|
||||||
|
if (state.inFunction && !state.currentFunction.empty()) {
|
||||||
|
state.functions[state.currentFunction].push_back(varName);
|
||||||
|
std::cout << " FUNC PRINT " << state.currentFunction << ": " << varName << "\n";
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
state.globalPrints.push_back(varName);
|
||||||
|
std::cout << " PRINT " << varName << "\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// WYWOLYWANIE FUNKCJI;
|
||||||
|
else if (line.length() > 3 && line.find("();") != std::string::npos) {
|
||||||
|
size_t openParen = line.find("(");
|
||||||
|
if (openParen != std::string::npos) {
|
||||||
|
std::string funcName = line.substr(0, openParen);
|
||||||
|
size_t nameStart = funcName.find_first_not_of(" \t");
|
||||||
|
size_t nameEnd = funcName.find_last_not_of(" \t");
|
||||||
|
funcName = funcName.substr(nameStart, nameEnd - nameStart + 1);
|
||||||
|
|
||||||
|
state.printCalls.push_back("CALL_" + funcName); // ZMIANA: state
|
||||||
|
std::cout << " CALL FUNC " << funcName << "\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void calculateExpressions(CompilerState& state) {
|
||||||
|
for (const auto& expr : state.expressions) {
|
||||||
|
if (state.variables.count(expr.leftVar) && state.variables.count(expr.rightVar)) {
|
||||||
|
int left = state.variables[expr.leftVar];
|
||||||
|
int right = state.variables[expr.rightVar];
|
||||||
|
int result = 0;
|
||||||
|
if (expr.op == "+") result = left + right;
|
||||||
|
else if (expr.op == "==") result = (left == right);
|
||||||
|
state.variables[expr.resultVar] = result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
7
PCCcompiler/parser.h
Normal file
7
PCCcompiler/parser.h
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
#ifndef PARSER_H
|
||||||
|
#define PARSER_H
|
||||||
|
#include "compiler_types.h"
|
||||||
|
|
||||||
|
void processSource(const std::string& src, CompilerState& state);
|
||||||
|
void calculateExpressions(CompilerState& state);
|
||||||
|
#endif
|
||||||
8
PCCcompiler/utils.cpp
Normal file
8
PCCcompiler/utils.cpp
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
#include "utils.h"
|
||||||
|
|
||||||
|
std::string trim(const std::string& str) {
|
||||||
|
size_t first = str.find_first_not_of(" \t");
|
||||||
|
if (first == std::string::npos) return "";
|
||||||
|
size_t last = str.find_last_not_of(" \t");
|
||||||
|
return str.substr(first, last - first + 1);
|
||||||
|
}
|
||||||
6
PCCcompiler/utils.h
Normal file
6
PCCcompiler/utils.h
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
#ifndef UTILS_H
|
||||||
|
#define UTILS_H
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
std::string trim(const std::string& str);
|
||||||
|
#endif
|
||||||
Reference in New Issue
Block a user