using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Collections.Concurrent;
namespace SwScript.CodeCompiler //SwScript Version 3.0
{
public class Interpreter
{
private ConcurrentDictionary<string, object> variables = new ConcurrentDictionary<string, object>(); // Use object to store different types
private ConcurrentDictionary<string, string> library = new ConcurrentDictionary<string, string>(); // Dictionary to store library functions
private Form form;
public void Execute(string code, TextBox tex)
{
// Split the code into lines
string[] lines = code.Split('\n');
// Iterate over each line
foreach (string line in lines)
{
// Remove whitespace
string trimmedLine = line.Trim();
// Check if the line is a comment
if (trimmedLine.StartsWith("//"))
{
continue;
}
// Parse the line using regular expressions
Match match = Regex.Match(trimmedLine, @"^(?<command>\w+)\s*(?<arguments>.*)$");
if (match.Success)
{
string command = match.Groups["command"].Value;
string arguments = match.Groups["arguments"].Value;
// Check if the line is a variable declaration
if (command == "let")
{
// Get the variable name and type
string[] parts = arguments.Split(' ');
if (parts.Length >= 3)
{
string variableName = parts[0];
string variableType = parts[1];
string variableValue = parts[2];
// Add the variable to the dictionary based on type
if (variableType == "int")
{
// Check if the value contains a '+' sign
if (variableValue.Contains('+'))
{
// Split the value by '+'
string[] valueParts = variableValue.Split('+');
if (valueParts.Length == 2)
{
// Parse the parts as integers and add them
int value1 = int.Parse(valueParts[0]);
int value2 = int.Parse(valueParts[1]);
variables.TryAdd(variableName, value1 + value2);
}
else
{
MessageBox.Show("Invalid variable value: " + variableValue);
}
}
else
{
variables.TryAdd(variableName, int.Parse(variableValue));
}
}
else if (variableType == "string")
{
variables.TryAdd(variableName, variableValue);
}
else if (variableType == "float")
{
variables.TryAdd(variableName, float.Parse(variableValue));
}
else
{
MessageBox.Show("Invalid data type: " + variableType);
}
}
else
{
MessageBox.Show("Invalid variable declaration: " + trimmedLine);
}
}
// Check if the line is a variable assignment
else if (command == "set")
{
// Get the variable name and value
string[] parts = arguments.Split(' ');
if (parts.Length == 2)
{
string variableName = parts[0];
string variableValue = parts[1];
// Update the variable in the dictionary based on type
if (variables.ContainsKey(variableName))
{
if (variables[variableName] is int)
{
// Check if the value contains a '+' sign
if (variableValue.Contains('+'))
{
// Split the value by '+'
string[] valueParts = variableValue.Split('+');
if (valueParts.Length == 2)
{
// Parse the parts as integers and add them
int value1 = int.Parse(valueParts[0]);
int value2 = int.Parse(valueParts[1]);
variables[variableName] = value1 + value2;
}
else
{
MessageBox.Show("Invalid variable value: " + variableValue);
}
}
else
{
variables[variableName] = int.Parse(variableValue);
}
}
else if (variables[variableName] is string)
{
variables[variableName] = variableValue;
}
else if (variables[variableName] is float)
{
variables[variableName] = float.Parse(variableValue);
}
}
else
{
tex.Text = "Variable not declared: " + variableName;
}
}
else
{
tex.Text = "Invalid variable assignment: " + trimmedLine;
}
}
// Check if the line is a print statement
else if (command == "print")
{
// Get the value to print
string valueToPrint = arguments;
// Check if the value is a variable
if (variables.ContainsKey(valueToPrint))
{
// Print the variable value
tex.Text += variables[valueToPrint].ToString() + Environment.NewLine; // Add newline after each print
}
else
{
// Print the value directly
tex.Text += valueToPrint + Environment.NewLine; // Add newline after each print
}
}
// Check if the line is an if statement
else if (command == "if")
{
// Get the condition and code block
string[] parts = arguments.Split(':');
if (parts.Length == 2)
{
string condition = parts[0].Trim();
string codeBlock = parts[1].Trim();
// Evaluate the condition
if (EvaluateCondition(condition))
{
// Execute the code block
Execute(codeBlock, tex);
}
}
else
{
tex.Text = "Invalid if statement: " + trimmedLine;
}
}
// Check if the line is an else if statement
else if (command == "else if")
{
// Get the condition and code block
string[] parts = arguments.Split(':');
if (parts.Length == 2)
{
string condition = parts[0].Trim();
string codeBlock = parts[1].Trim();
// Evaluate the condition
if (EvaluateCondition(condition))
{
// Execute the code block
Execute(codeBlock, tex);
}
}
else
{
tex.Text = "Invalid else if statement: " + trimmedLine;
}
}
// Check if the line is an else statement
else if (command == "else")
{
// Get the code block
string codeBlock = arguments.Trim();
// Execute the code block
Execute(codeBlock, tex);
}
else if (command == "newForm")
{
// Get the form name
string formName = arguments.Trim();
// Create an instance of the form
form = new Form();
form.ShowIcon = false;
form.Text = formName;
form.Show();
}
else if (command == "use")
{
// Get the library name
string libraryName = arguments.Trim();
// Open the library file
string libraryPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, libraryName + ".sws");
if (File.Exists(libraryPath))
{
// Read the library file
string libraryCode = File.ReadAllText(libraryPath);
// Add the library functions to the dictionary
AddLibraryFunctions(libraryCode);
// Execute the library code
Execute(libraryCode, tex);
}
else
{
tex.Text = "Library not found: " + libraryName;
}
}
// Check if the line is a library function call
else if (library.ContainsKey(command))
{
// Get the library function code
string functionCode = library[command];
// Replace the function parameters with actual values
functionCode = ReplaceParameters(functionCode, arguments);
// Execute the function code
Execute(functionCode, tex);
}
else if(command == "newMessage")
{
string text = arguments.Trim();
MessageBox.Show(text,"Message");
}
else if (command == "newButton")
{
string[] parts = arguments.Split(',');
if (parts.Length >= 2)
{
string btnText = parts[0].Trim();
string btnLocation = parts[1].Trim();
// Create and configure the button
Button btn = new Button();
btn.Text = btnText;
// Set the location of the button
string[] locationParts = btnLocation.Split(' ');
if (locationParts.Length == 2)
{
int x = int.Parse(locationParts[0]);
int y = int.Parse(locationParts[1]);
btn.Location = new System.Drawing.Point(x, y);
}
if (form != null)
{
form.Controls.Add(btn);
}
else
{
MessageBox.Show("No form available to add button.");
}
}
}
else
{
// Invalid command
tex.Text = "Invalid command: " + command;
}
}
else
{
// Invalid line
tex.Text = "Çıktı: " + trimmedLine;
}
}
}
// Method to add library functions to the dictionary
private void AddLibraryFunctions(string libraryCode)
{
// Split the library code into lines
string[] lines = libraryCode.Split('\n');
// Iterate over each line
foreach (string line in lines)
{
// Remove whitespace
string trimmedLine = line.Trim();
// Check if the line is a function declaration
if (trimmedLine.StartsWith("func"))
{
// Get the function name and code
string[] parts = trimmedLine.Split(' ');
if (parts.Length >= 3)
{
string functionName = parts[1];
string functionCode = string.Join(" ", parts.Skip(2));
// Add the function to the dictionary
library.TryAdd(functionName, functionCode);
}
}
}
}
// Method to replace function parameters with actual values
private string ReplaceParameters(string functionCode, string arguments)
{
// Split the arguments into parts
string[] parts = arguments.Split(',');
// Iterate over each part
for (int i = 0; i < parts.Length; i++)
{
// Replace the parameter with the actual value
functionCode = functionCode.Replace("$" + (i + 1), parts[i].Trim());
}
return functionCode;
}
// Method to evaluate conditions
// Method to evaluate conditions
private bool EvaluateCondition(string condition)
{
// Koşul ifadesini parçalara ayır
string[] parts = condition.Split(' ');
// Değişkenleri kontrol et
if (variables.ContainsKey(parts[0]))
{
// Değişkenin değerini al
object value1 = variables[parts[0]];
// Operatörü kontrol et
if (parts[1] == ">")
{
// Sağ taraftaki değeri kontrol et
if (variables.ContainsKey(parts[2]))
{
// Değişkenin değerini al
object value2 = variables[parts[2]];
// Değerleri karşılaştır
if (value1 is int && value2 is int)
{
return (int)value1 > (int)value2;
}
else if (value1 is string && value2 is string)
{
return ((string)value1).CompareTo((string)value2) > 0;
}
else if (value1 is float && value2 is float)
{
return (float)value1 > (float)value2;
}
else
{
MessageBox.Show("Hatalı karşılaştırma: " + condition);
return false;
}
}
else
{
// Sağ taraftaki değeri sayı olarak parse et
try
{
int value2 = int.Parse(parts[2]);
// Değerleri karşılaştır
if (value1 is int)
{
return (int)value1 > value2;
}
else if (value1 is float)
{
return (float)value1 > value2;
}
else
{
MessageBox.Show("Hatalı karşılaştırma: " + condition);
return false;
}
}
catch (FormatException)
{
MessageBox.Show("Hatalı karşılaştırma: " + condition);
return false;
}
}
}
else if (parts[1] == "==")
{
// Sağ taraftaki değeri kontrol et
if (variables.ContainsKey(parts[2]))
{
// Değişkenin değerini al
object value2 = variables[parts[2]];
// Değerleri karşılaştır
if (value1 is int && value2 is int)
{
return (int)value1 == (int)value2;
}
else if (value1 is string && value2 is string)
{
return ((string)value1).CompareTo((string)value2) == 0;
}
else if (value1 is float && value2 is float)
{
return (float)value1 == (float)value2;
}
else
{
MessageBox.Show("Hatalı karşılaştırma: " + condition);
return false;
}
}
else
{
// Sağ taraftaki değeri sayı olarak parse et
try
{
int value2 = int.Parse(parts[2]);
// Değerleri karşılaştır
if (value1 is int)
{
return (int)value1 == value2;
}
else if (value1 is float)
{
return (float)value1 == value2;
}
else
{
MessageBox.Show("Hatalı karşılaştırma: " + condition);
return false;
}
}
catch (FormatException)
{
MessageBox.Show("Hatalı karşılaştırma: " + condition);
return false;
}
}
}
else if (parts[1] == "<")
{
// Sağ taraftaki değeri kontrol et
if (variables.ContainsKey(parts[2]))
{
// Değişkenin değerini al
object value2 = variables[parts[2]];
// Değerleri karşılaştır
if (value1 is int && value2 is int)
{
return (int)value1 < (int)value2;
}
else if (value1 is string && value2 is string)
{
return ((string)value1).CompareTo((string)value2) < 0;
}
else if (value1 is float && value2 is float)
{
return (float)value1 < (float)value2;
}
else
{
MessageBox.Show("Hatalı karşılaştırma: " + condition);
return false;
}
}
else
{
// Sağ taraftaki değeri sayı olarak parse et
try
{
int value2 = int.Parse(parts[2]);
// Değerleri karşılaştır
if (value1 is int)
{
return (int)value1 < value2;
}
else if (value1 is float)
{
return (float)value1 < value2;
}
else
{
MessageBox.Show("Hatalı karşılaştırma: " + condition);
return false;
}
}
catch (FormatException)
{
MessageBox.Show("Hatalı karşılaştırma: " + condition);
return false;
}
}
}
else if (parts[1] == "<=")
{
// Sağ taraftaki değeri kontrol et
if (variables.ContainsKey(parts[2]))
{
// Değişkenin değerini al
object value2 = variables[parts[2]];
// Değerleri karşılaştır
if (value1 is int && value2 is int)
{
return (int)value1 <= (int)value2;
}
else if (value1 is string && value2 is string)
{
return ((string)value1).CompareTo((string)value2) <= 0;
}
else if (value1 is float && value2 is float)
{
return (float)value1 <= (float)value2;
}
else
{
MessageBox.Show("Hatalı karşılaştırma: " + condition);
return false;
}
}
else
{
// Sağ taraftaki değeri sayı olarak parse et
try
{
int value2 = int.Parse(parts[2]);
// Değerleri karşılaştır
if (value1 is int)
{
return (int)value1 <= value2;
}
else if (value1 is float)
{
return (float)value1 <= value2;
}
else
{
MessageBox.Show("Hatalı karşılaştırma: " + condition);
return false;
}
}
catch (FormatException)
{
MessageBox.Show("Hatalı karşılaştırma: " + condition);
return false;
}
}
}
else if (parts[1] == ">=")
{
// Sağ taraftaki değeri kontrol et
if (variables.ContainsKey(parts[2]))
{
// Değişkenin değerini al
object value2 = variables[parts[2]];
// Değerleri karşılaştır
if (value1 is int && value2 is int)
{
return (int)value1 >= (int)value2;
}
else if (value1 is string && value2 is string)
{
return ((string)value1).CompareTo((string)value2) >= 0;
}
else if (value1 is float && value2 is float)
{
return (float)value1 >= (float)value2;
}
else
{
MessageBox.Show("Hatalı karşılaştırma: " + condition);
return false;
}
}
else
{
// Sağ taraftaki değeri sayı olarak parse et
try
{
int value2 = int.Parse(parts[2]);
// Değerleri karşılaştır
if (value1 is int)
{
return (int)value1 >= value2;
}
else if (value1 is float)
{
return (float)value1 >= value2;
}
else
{
MessageBox.Show("Hatalı karşılaştırma: " + condition);
return false;
}
}
catch (FormatException)
{
MessageBox.Show("Hatalı karşılaştırma: " + condition);
return false;
}
}
}
else if (parts[1] == "!=")
{
// Sağ taraftaki değeri kontrol et
if (variables.ContainsKey(parts[2]))
{
// Değişkenin değerini al
object value2 = variables[parts[2]];
// Değerleri karşılaştır
if (value1 is int && value2 is int)
{
return (int)value1 != (int)value2;
}
else if (value1 is string && value2 is string)
{
return ((string)value1).CompareTo((string)value2) != 0;
}
else if (value1 is float && value2 is float)
{
return (float)value1 != (float)value2;
}
else
{
MessageBox.Show("Hatalı karşılaştırma: " + condition);
return false;
}
}
else
{
// Sağ taraftaki değeri sayı olarak parse et
try
{
int value2 = int.Parse(parts[2]);
// Değerleri karşılaştır
if (value1 is int)
{
return (int)value1 != value2;
}
else if (value1 is float)
{
return (float)value1 != value2;
}
else
{
MessageBox.Show("Hatalı karşılaştırma: " + condition);
return false;
}
}
catch (FormatException)
{
MessageBox.Show("Hatalı karşılaştırma: " + condition);
return false;
}
}
}
else
{
MessageBox.Show("Geçersiz operatör: " + parts[1]);
return false;
}
}
else
{
MessageBox.Show("Değişken bulunamadı: " + parts[0]);
return false;
}
}
}
}