// Generated by Oraset3 Compiler
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <cmath>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <sstream>
#ifdef _WIN32
#include <windows.h>
#include <conio.h>
#include <commdlg.h>
#else
#include <unistd.h>
#include <sys/ioctl.h>
#include <termios.h>
#endif

using namespace std;

// 设置控制台编码为 UTF-8
#ifdef _WIN32
struct ConsoleInitializer {
    ConsoleInitializer() {
        SetConsoleOutputCP(65001);
        SetConsoleCP(65001);
    }
};
static ConsoleInitializer _consoleInit;
#endif

// Built-in functions
void print(string value);
void println(string value);
void print(int value);
void println(int value);
void print(float value);
void println(float value);
void print(bool value);
void println(bool value);

// System functions
int sys(string cmd);

// File functions
string file_read(string path);
void file_write(string path, string content);
bool file_exists(string path);

// Screen functions
void screen_clear();
string screen_size();

// Draw functions
void draw_text(int x, int y, string text);
void draw_rect(int x, int y, int width, int height);
void draw_line(int x1, int y1, int x2, int y2);
void draw_set_color(int r, int g, int b);

enum Color {
RED,    
GREEN,    
BLUE = 10,    
YELLOW    
};

union Data {
    int i;
    float f;
    bool b;
};


int main() {
println(string("=== Oraset3 操作系统级编程特性 ==="))    ;
println(string(""))    ;
println(string("1. 位操作符测试:"))    ;
auto a = 10    ;
auto b = 12    ;
println(string("a = 10 (0b1010)"))    ;
println(string("b = 12 (0b1100)"))    ;
println(string("a & b ="))    ;
println((a & b))    ;
println(string("a | b ="))    ;
println((a | b))    ;
println(string("a ^ b ="))    ;
println((a ^ b))    ;
println(string("a << 1 ="))    ;
println((a << 1))    ;
println(string("a >> 1 ="))    ;
println((a >> 1))    ;
println(string(""))    ;
println(string("2. 枚举测试:"))    ;
auto c = RED    ;
println(string("Color.RED ="))    ;
println(c)    ;
println(string("Color.BLUE ="))    ;
println(BLUE)    ;
println(string(""))    ;
println(string("3. 联合体测试:"))    ;
Data data    ;
data.i = 42    ;
println(string("data.i ="))    ;
println(data.i)    ;
println(string(""))    ;
println(string("4. 指针操作测试:"))    ;
auto x = 100    ;
auto ptr = &x    ;
println(string("x ="))    ;
println(x)    ;
println(string("*ptr ="))    ;
(println(*ptr) * ptr) = 200    ;
println(string("修改后 x ="))    ;
println(x)    ;
println(string(""))    ;
println(string("5. 类型转换测试:"))    ;
auto num = 3.14    ;
auto intNum = int    ;
num    ;
println(string("(int)3.14 ="))    ;
println(intNum)    ;
println(string(""))    ;
println(string("=== 操作系统级特性测试完成 ==="))    ;
    return 0;
}

// Built-in function implementations
void println(string s) { printf("%s\n", s.c_str()); }
void print(string s) { printf("%s", s.c_str()); }
void print(int value) { printf("%d", value); }
void println(int value) { printf("%d\n", value); }
void print(float value) { printf("%g", value); }
void println(float value) { printf("%g\n", value); }
void print(bool value) { printf("%s", value ? "true" : "false"); }
void println(bool value) { printf("%s\n", value ? "true" : "false"); }

// System function implementations
int sys(string cmd) {
    return system(cmd.c_str());
}

// File function implementations
string file_read(string path) {
    ifstream file(path);
    if (!file) return "";
    stringstream buffer;
    buffer << file.rdbuf();
    return buffer.str();
}
void file_write(string path, string content) {
    ofstream file(path);
    if (file) {
        file << content;
    }
}
bool file_exists(string path) {
    ifstream file(path);
    return file.good();
}

// Screen function implementations
void screen_clear() {
#ifdef _WIN32
    system("cls");
#else
    system("clear");
#endif
}
string screen_size() {
#ifdef _WIN32
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
    int width = csbi.srWindow.Right - csbi.srWindow.Left + 1;
    int height = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
    return to_string(width) + "x" + to_string(height);
#else
    struct winsize w;
    ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
    return to_string(w.ws_col) + "x" + to_string(w.ws_row);
#endif
}

// Draw function implementations
void draw_text(int x, int y, string text) {
#ifdef _WIN32
    COORD coord;
    coord.X = x;
    coord.Y = y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
    printf("%s", text.c_str());
#else
    printf("\033[%d;%dH%s", y+1, x+1, text.c_str());
#endif
}
void draw_rect(int x, int y, int width, int height) {
    for (int row = y; row < y + height; row++) {
        for (int col = x; col < x + width; col++) {
            if (row == y || row == y + height - 1 || col == x || col == x + width - 1) {
                draw_text(col, row, "*");
            }
        }
    }
}
void draw_line(int x1, int y1, int x2, int y2) {
    int dx = abs(x2 - x1);
    int dy = abs(y2 - y1);
    int sx = (x1 < x2) ? 1 : -1;
    int sy = (y1 < y2) ? 1 : -1;
    int err = dx - dy;
    int x = x1, y = y1;
    while (true) {
        draw_text(x, y, ".");
        if (x == x2 && y == y2) break;
        int e2 = 2 * err;
        if (e2 > -dy) { err -= dy; x += sx; }
        if (e2 < dx) { err += dx; y += sy; }
    }
}
void draw_set_color(int r, int g, int b) {
#ifdef _WIN32
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    WORD color = ((r / 51) << 5) | ((g / 51) << 2) | (b / 51);
    SetConsoleTextAttribute(hConsole, color);
#else
    printf("\033[38;2;%d;%d;%dm", r, g, b);
#endif
}

