Coding/CPP 삽질기2023. 7. 1. 18:03

주말 동안 C# 을 공부 하기로 하고, 첫 프로그램을 작성해 보았다.

언어을 배우면 누구나 만들어 보는 Word Count 프로그램.

class WordCount
{
    private readonly string fileText;

    WordCount(string fileText)
    {
        this.fileText = fileText;
    }

    private void printInfo()
    {
        var wordCount = getWordCount(fileText);
        var lineCount = getLineCount(fileText);

        Console.WriteLine($"LineCount: {lineCount}");
        Console.WriteLine($"WordCount: {wordCount}");
        Console.WriteLine($"Character Count: {fileText.Length}");
    }

    private int getWordCount(string fileText)
    {
        return fileText.Split(new[] { ' ', '\t', '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries).Length;
    }
    
    private int getLineCount(string fileText)
    {
        return fileText.Split(new[] { '\n' }).Length;
    }
    
    static void Main(string[] args)
    {
        if (args.Length == 0)
        {
            Console.WriteLine("Usage: WordCount [FileName]");    
            return;
        }

        string filePath = args[0];
        if (!File.Exists(filePath))
        {
            Console.WriteLine($"{filePath} not exist!");
            return;
        }

        string fileText = File.ReadAllText(filePath);
        WordCount wc = new WordCount(fileText);
        wc.printInfo();
    }
}

'Coding > CPP 삽질기' 카테고리의 다른 글

[C#] Visual studio 도구 상자 활성화 방법  (0) 2023.07.03
Intellij Code snippet 사용법  (0) 2023.06.13
[CPP] 펜윅 트리 (Fenwick Tree)  (0) 2023.06.09
Posted by chobocho
Coding/CPP 삽질기2023. 6. 13. 23:50
Coding/Python 삽질기2023. 6. 9. 23:28

1. Python 3.10.6 설치 

https://www.python.org/downloads/release/python-3106/

 

Python Release Python 3.10.6

The official home of the Python Programming Language

www.python.org

2.  Git 설치

https://git-scm.com/downloads

 

Git - Downloads

Downloads macOS Windows Linux/Unix Older releases are available and the Git source repository is on GitHub. GUI Clients Git comes with built-in GUI tools (git-gui, gitk), but there are several third-party tools for users looking for a platform-specific exp

git-scm.com

3. Stable diffusion webui 설치

https://github.com/AUTOMATIC1111/stable-diffusion-webui.git

 

GitHub - AUTOMATIC1111/stable-diffusion-webui: Stable Diffusion web UI

Stable Diffusion web UI. Contribute to AUTOMATIC1111/stable-diffusion-webui development by creating an account on GitHub.

github.com

git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui

4. 모델 다운로드

https://civitai.com/

 

Civitai | Stable Diffusion models, embeddings, LoRAs and more

Civitai is a platform for Stable Diffusion AI Art models. Browse a collection of thousands of models from a growing number of creators. Join an engaged community in reviewing models and sharing images with prompts to get you started.

civitai.com

https://huggingface.co/WarriorMama777/OrangeMixs

 

WarriorMama777/OrangeMixs · Hugging Face

AOM3 Counterfeit2.5 Add SUM @ 1.0 0,0.6,0.6,0.6,0.6,0.6,0,0,0,0,0.6,0.1,0.6,0.6,0.6,0.6,0.6,0.5,0.1,0.1,0.6,0.6,0.2,0.6,0.6,0.6 AOM3A3

huggingface.co

 

모델을 다운 받아서, \stable-diffusion-webui\models\Stable-diffusion 에 넣어 준다.

확장자가 .safetensors 인 것을 받는 걸 추천 한다.

5. 실행

webui-user.bat 를 수행한다. (첫 실행시 이것 저것 설치한다고 시간이 걸린다.)

6. http://127.0.0.1:7860/ 로 웹 브라우저로 접속 하면 된다.

'Coding > Python 삽질기' 카테고리의 다른 글

HTTP Protocol  (0) 2023.08.18
[Python] PyTorch 설치  (0) 2023.04.05
[ChatGPT에게 묻다] python으로 Simple Stack VM 만들기 (3)  (0) 2023.03.17
Posted by chobocho
Coding/CPP 삽질기2023. 6. 9. 23:14
#include <iostream>

const int MAX_SIZE = 10;

void update(int* TREE, int idx, int value) {
    for (++idx; idx <= MAX_SIZE+1; idx += (idx & -idx))
        TREE[idx] += value;
}

int sum(int *TREE, int s) {
    int r = 0;
    for (++s; s > 0; s &= (s-1))
        r += TREE[s];
    return r;
}

int main(int argc, char **argv) {
    int TBL[MAX_SIZE] = {0};
    int TREE[MAX_SIZE+1] = {0, };

    for (auto i = 0; i < MAX_SIZE; i++) {
        TBL[i] = i+1;
        update(TREE, i, TBL[i]);
    }

    // GET SUM from 1 ~ N
    for (auto i = 0; i < MAX_SIZE; i++)
        std::cout << sum(TREE, i) << ", ";
    std::cout << std::endl;
    
    return 0;
}
Posted by chobocho
Coding/CPP 삽질기2023. 6. 7. 00:59

문득 기초 코딩(?) 실력이 떨어진것 같아서, 프로그래머스의  Level 0 문제를 C로 모두 풀어 보았다.

중간 중간 예상외로 막히는(?) 문제를 만나면서, 잊고 있었던 것들이 많았다는 걸 배웠다.

심심하신 분들에게 추천합니다.

'Coding > CPP 삽질기' 카테고리의 다른 글

[CPP] 펜윅 트리 (Fenwick Tree)  (0) 2023.06.09
[ChatGPT에게 묻다] Fast sqrt 함수  (0) 2023.02.26
별 그리기  (0) 2021.06.08
Posted by chobocho
Coding/Java 삽질기2023. 5. 10. 19:22

Classic Block Game V2 는 어떠한 개인 정보도 수집하지 않습니다.

Classic Block Game V2 does not collect any personal information.

Posted by chobocho

1. 시작하기

   1.1. 테트리스 게임 소개

테트리스는 누구나 한 번쯤 들어 본 게임이기에, 설명은 생략 합니다.

테트리스에 대한 자세한 사항은 아래 링크를 참고 하시기 바랍니다.

 

   1.2. 구현 할 테트리스 게임의 요구사항 정의

우리가 만들 테트리스 게임은 아래 사이트에서 플레이 할 수 있습니다.

 

Chobosho's tetris

Tetris!

chobocho.com

 

안드로이드로 포팅한 버전은 아래 사이트에서 플레이 할 수 있습니다.

 

Classic Block Game V2 - Google Play 앱

안드로이드용 클래식 블록 게임입니다.

play.google.com

 

그리고, 전체 소스는 아래에서 다운 받을 수 있습니다.

 

이번에 구현 할 테트리스 게임은 아케이드 모드, 퍼즐 모드, 아이템 모드 총 3개의 모드로 구성이 됩니다.

 

1.2.1 아케이드 모드

테트리스의 기본 형태로, 모두가 잘 알고 있는 내용이라, 자세한 설명은 생략합니다.

1.2.2 퍼즐모드

퍼즐 모드 테트리스는 고전적인 테트리스 게임을 벗어나, 플레이어의 두뇌와 반응 속도를 동시에 시험하는 독특한 경험을 제공합니다. 이 게임에는 70가지 이상의의 다양한 퍼즐이 포함되어 있어, 게임을 진행하면서 플레이어의 전략적 사고력을 향상시키는 동시에 문제 해결 능력을 증진시키는 데 도움이 됩니다.
각 스테이지의 목표는 화면에 보이는 색상이 있는 블록을 모두 제거하는 것입니다. 이는 단순히 블록을 빠르게 쌓는 것을 넘어서, 플레이어가 어떻게 블록을 배치하고 어떤 블록을 사용할지를 신중하게 고려해야 하는 전략적인 요소를 게임에 도입합니다.

1.2.3 아이템 모드

아이템 모드는 다양한 아이템들이 포함된 전략적인 게임을 제공 합니다. 이 모드의 주요 아이템들은 Yellow Thunder, Black Thunder, Orange Thunder, Red Thunder, Blue Boom, Black Boom, Green Boom, 그리고 Red Boom입니다.

Yellow Thunder는 화면의 모든 검은 폭탄을 폭발시키며, Black Thunder는 최대 3개의 폭탄을 화면에 추가합니다. Orange Thunder는 아이콘이 바닥에 닿을 때 중심을 기준으로 3x3 영역을 지웁니다. 단, 아이콘이 포함된 줄이 꽉 차있다면 작동하지 않습니다. Red Thunder는 아이콘이 바닥에 닿으면 현재 위치한 세로 라인과 오른쪽 세로 라인을 지웁니다.

Blue Boom은 아이콘이 바닥에 닿았을 때 중심 주변 3x3 크기를 회색 블록으로 채웁니다. Black Boom은 폭탄이 포함된 줄이 블록으로 꽉 차면 위 아래 한 줄씩 총 3줄을 삭제합니다. Green Boom은 원형의 회색 블록을 추가합니다. 단, 아이콘이 포함된 줄이 꽉 차있다면 작동하지 않습니다. Orange Boom은 아이콘 중심으로 빈 3x3 사각형을 그립니다. 마찬가지로, 아이콘이 포함된 줄이 꽉 차있다면 작동하지 않습니다. Red Boom은 아이콘이 위치한 한 줄을 지웁니다.

아이템 테트리스는 이러한 독특한 아이템들을 활용해 주어진 판의 블록들을 모두 제거하면서, 전략적으로 높은 점수를 달성하는 것이 목표입니다.

플레이어들은 다양한 아이템을 사용하여 게임을 더욱 재미있게 즐길 수 있습니다.

 

Posted by chobocho
Coding/JavsScript 삽질기2023. 4. 18. 00:16

게임 플레이 해보기
http://www.chobocho.com/game/tetris/

 

Chobosho's tetris

Tetris!

chobocho.com

 

자바스크립트를 공부하면서 만든 아이템 테트리스 소스 입니다.
인터넷을 대충 뒤져 보니, 간단한 테트리스 소스는 많으나,
퍼즐 테트리스, 아이템 테트리스 소스는 없어서 공개 합니다.


 



소스코드 위치
https://github.com/chobocho/webTetris/tree/item_tetris

 

GitHub - chobocho/webTetris: Javascript tetris

Javascript tetris. Contribute to chobocho/webTetris development by creating an account on GitHub.

github.com

좋아요, 추천은 힘이 됩니다! ^^
 

Posted by chobocho
Coding/Python 삽질기2023. 4. 5. 01:25

1. 아래 사이트에서  설치해야 하는 PyTorch 버전 체크

https://pytorch.org/get-started/locally/

 

PyTorch

An open source machine learning framework that accelerates the path from research prototyping to production deployment.

pytorch.org

2. Command를 복사하여 실행한다.

pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu117

3.  Python을 실행하여 확인 한다.

 

Posted by chobocho
Coding/Python 삽질기2023. 3. 17. 01:20

2부에 이어서, 3부를 시작 합니다.

 

8. 이제 생성된 코드를 리팩토링 해달라고 부탁 합니다.

from enum import Enum

class Command(Enum):
    PUSH = 'PUSH'
    POP = 'POP'
    ADD = 'ADD'
    SUB = 'SUB'
    MUL = 'MUL'
    DIV = 'DIV'
    MOD = 'MOD'
    DUP = 'DUP'
    SWAP = 'SWAP'
    JMP = 'JMP'
    JG = 'JG'
    JL = 'JL'
    JZ = 'JZ'
    JNZ = 'JNZ'
    CMP = 'CMP'
    MOV = 'MOV'
    NOP = 'NOP'
    HALT = 'HALT'


class StackMachine:
    """
    PUSH <value>: Push a value onto the stack. e.g., PUSH 10
    POP: Remove and return the top value from the stack.
    ADD: Remove the top two values from the stack, calculate their sum, and push the result onto the stack.
    SUB: Remove the top two values from the stack, calculate their difference, and push the result onto the stack.
    MUL: Remove the top two values from the stack, calculate their product, and push the result onto the stack.
    DIV: Remove the top two values from the stack, calculate their division, and push the result onto the stack.
    MOD: Remove the top two values from the stack, calculate their remainder, and push the result onto the stack.
    DUP: Copy the top value from the stack and push it onto the stack.
    SWAP: Swap the top two values on the stack.
    JMP <label>: Move program execution to the given label. e.g., JMP LOOP
    JG <label>: If the top value on the stack is greater than 0, move program execution to the given label.
    JL <label>: If the top value on the stack is less than 0, move program execution to the given label.
    JZ <label>: If the top value on the stack is 0, move program execution to the given label.
    JNZ <label>: If the top value on the stack is not 0, move program execution to the given label.
    CMP: Compare the top two values on the stack and push the result onto the stack.
    (-1: First value is smaller, 0: Both values are equal, 1: First value is larger)
    MOV <register>: Move the top value from the stack to the specified register (AX, BX, CX, or DX).
    NOP: Perform no operation. This command is ignored in the program.
    HALT: Stop program execution.
    """

    def __init__(self):
        self.stack = []
        self.program = []
        self.labels = {}
        self.registers = {'AX': 0, 'BX': 0, 'CX': 0, 'DX': 0}
        self.instruction_pointer = 0

    def load_program(self, program):
        self.program = program
        self.labels = {line[:-1]: i for i, line in enumerate(program) if line.endswith(':')}

    def parse_command(self, command_string):
        command, *args = command_string.split()
        if command not in Command.__members__:
            raise ValueError(f"Invalid command: {command}")
        return Command[command], args

    def execute_command(self, command, args):
        if command == Command.PUSH:
            value = int(args[0]) if 'X' not in args[0] else self.registers[args[0]]
            self.stack.append(value)
        elif command == Command.POP:
            self.stack.pop()
        elif command == Command.ADD:
            self.stack.append(self.stack.pop() + self.stack.pop())
        elif command == Command.SUB:
            b, a = self.stack.pop(), self.stack.pop()
            self.stack.append(a - b)
        elif command == Command.MUL:
            self.stack.append(self.stack.pop() * self.stack.pop())
        elif command == Command.DIV:
            b, a = self.stack.pop(), self.stack.pop()
            self.stack.append(a // b)
        elif command == Command.MOD:
            b, a = self.stack.pop(), self.stack.pop()
            self.stack.append(a % b)
        elif command == Command.DUP:
            self.stack.append(self.stack[-1])
        elif command == Command.SWAP:
            self.stack[-1], self.stack[-2] = self.stack[-2], self.stack[-1]
        elif command == Command.JMP:
            self.instruction_pointer = self.labels[args[0]]
            return
        elif command == Command.JG:
            if self.stack.pop() == 1:
                self.instruction_pointer = self.labels[args[0]]
                return
        elif command == Command.JL:
            if self.stack.pop() == -1:
                self.instruction_pointer = self.labels[args[0]]
                return
        elif command == Command.JZ:
            if self.stack.pop() == 0:
                self.instruction_pointer = self.labels[args[0]]
                return
        elif command == Command.JNZ:
            if self.stack.pop() != 0:
                self.instruction_pointer = self.labels[args[0]]
                return
        elif command == Command.CMP:
            b, a = self.stack.pop(), self.stack.pop()
            self.stack.append((a > b) - (a < b))
        elif command == Command.MOV:
            self.registers[args[0]] = self.stack[-1]
        elif command == Command.NOP:
            pass
        elif command == Command.HALT:
            return
        else:
            raise ValueError(f"Unknown command: {command}")

        self.instruction_pointer += 1

    def run(self):
        for _ in range(20000):
            if self.instruction_pointer >= len(self.program):
                break
            command_string = self.program[self.instruction_pointer]
            command, args = self.parse_command(command_string)
            self.execute_command(command, args)

생성된 코드를 돌려보면, 역시 안됩니다. 

디버깅 하면, Label 처리가 안되었을 알 수 있습니다. 

 

9. Label 처리를  위한 코드를 손으로 추가 하여 수정 합니다.

from enum import Enum


class Command(Enum):
    PUSH = 'PUSH'
    POP = 'POP'
    ADD = 'ADD'
    SUB = 'SUB'
    MUL = 'MUL'
    DIV = 'DIV'
    MOD = 'MOD'
    DUP = 'DUP'
    SWAP = 'SWAP'
    JMP = 'JMP'
    JG = 'JG'
    JL = 'JL'
    JZ = 'JZ'
    JNZ = 'JNZ'
    CMP = 'CMP'
    MOV = 'MOV'
    NOP = 'NOP'
    HALT = 'HALT'
    LABEL = 'LABEL'


class StackMachine:
    """
    PUSH <value>: Push a value onto the stack. e.g., PUSH 10
    POP: Remove and return the top value from the stack.
    ADD: Remove the top two values from the stack, calculate their sum, and push the result onto the stack.
    SUB: Remove the top two values from the stack, calculate their difference, and push the result onto the stack.
    MUL: Remove the top two values from the stack, calculate their product, and push the result onto the stack.
    DIV: Remove the top two values from the stack, calculate their division, and push the result onto the stack.
    MOD: Remove the top two values from the stack, calculate their remainder, and push the result onto the stack.
    DUP: Copy the top value from the stack and push it onto the stack.
    SWAP: Swap the top two values on the stack.
    JMP <label>: Move program execution to the given label. e.g., JMP LOOP
    JG <label>: If the top value on the stack is greater than 0, move program execution to the given label.
    JL <label>: If the top value on the stack is less than 0, move program execution to the given label.
    JZ <label>: If the top value on the stack is 0, move program execution to the given label.
    JNZ <label>: If the top value on the stack is not 0, move program execution to the given label.
    CMP: Compare the top two values on the stack and push the result onto the stack.
    (-1: First value is smaller, 0: Both values are equal, 1: First value is larger)
    MOV <register>: Move the top value from the stack to the specified register (AX, BX, CX, or DX).
    NOP: Perform no operation. This command is ignored in the program.
    HALT: Stop program execution.
    """

    def __init__(self):
        self.stack = []
        self.program = []
        self.labels = {}
        self.registers = {'AX': 0, 'BX': 0, 'CX': 0, 'DX': 0}
        self.instruction_pointer = 0

    def load_program(self, program):
        self.program = program
        self.labels = {line[:-1]: i for i, line in enumerate(program) if line.endswith(':')}

    def parse_command(self, command_string):
        command, *args = command_string.split()
        if command not in Command.__members__:
            if ':' in command:
                return Command['LABEL'], args
            else:
                raise ValueError(f"Unknown command: {command}")
        return Command[command], args

    def execute_command(self, command, args):
        if command == Command.PUSH:
            value = int(args[0]) if 'X' not in args[0] else self.registers[args[0]]
            self.stack.append(value)
        elif command == Command.POP:
            self.stack.pop()
        elif command == Command.ADD:
            self.stack.append(self.stack.pop() + self.stack.pop())
        elif command == Command.SUB:
            b, a = self.stack.pop(), self.stack.pop()
            self.stack.append(a - b)
        elif command == Command.MUL:
            self.stack.append(self.stack.pop() * self.stack.pop())
        elif command == Command.DIV:
            b, a = self.stack.pop(), self.stack.pop()
            self.stack.append(a // b)
        elif command == Command.MOD:
            b, a = self.stack.pop(), self.stack.pop()
            self.stack.append(a % b)
        elif command == Command.DUP:
            self.stack.append(self.stack[-1])
        elif command == Command.SWAP:
            self.stack[-1], self.stack[-2] = self.stack[-2], self.stack[-1]
        elif command == Command.JMP:
            self.instruction_pointer = self.labels[args[0]]
            return
        elif command == Command.JG:
            if self.stack.pop() == 1:
                self.instruction_pointer = self.labels[args[0]]
                return
        elif command == Command.JL:
            if self.stack.pop() == -1:
                self.instruction_pointer = self.labels[args[0]]
                return
        elif command == Command.JZ:
            if self.stack.pop() == 0:
                self.instruction_pointer = self.labels[args[0]]
                return
        elif command == Command.JNZ:
            if self.stack.pop() != 0:
                self.instruction_pointer = self.labels[args[0]]
                return
        elif command == Command.CMP:
            b, a = self.stack.pop(), self.stack.pop()
            self.stack.append((a > b) - (a < b))
        elif command == Command.MOV:
            self.registers[args[0]] = self.stack[-1]
        elif command == Command.NOP:
            pass
        elif command == Command.HALT:
            return
        elif command == Command.LABEL:
            pass
        else:
            raise ValueError(f"Unknown command: {command}")

        self.instruction_pointer += 1

    def run(self):
        for _ in range(20000):
            if self.instruction_pointer >= len(self.program):
                break
            command_string = self.program[self.instruction_pointer]
            command, args = self.parse_command(command_string)
            self.execute_command(command, args)

 

10.  execute_command() 함수 안의 수 많은 if - else 조합을 없애기 위해서 약간 코드를 고칩니다.

    def command_push(self, args):
        value = int(args[0]) if 'X' not in args[0] else self.registers[args[0]]
        self.stack.append(value)

    def command_pop(self, args):
        self.stack.pop()

위와 같이 함수를 2개 추가하고, 코드를 약간 고쳐 줍니다.

 

11. 수정 된 코드를 넣고, 리팩토링을 부탁합니다.

from enum import Enum


class Command(Enum):
    PUSH = 'PUSH'
    POP = 'POP'
    ADD = 'ADD'
    SUB = 'SUB'
    MUL = 'MUL'
    DIV = 'DIV'
    MOD = 'MOD'
    DUP = 'DUP'
    SWAP = 'SWAP'
    JMP = 'JMP'
    JG = 'JG'
    JL = 'JL'
    JZ = 'JZ'
    JNZ = 'JNZ'
    CMP = 'CMP'
    MOV = 'MOV'
    NOP = 'NOP'
    HALT = 'HALT'
    LABEL = 'LABEL'


class StackMachine:
    """
    PUSH <value>: Push a value onto the stack. e.g., PUSH 10
    POP: Remove and return the top value from the stack.
    ADD: Remove the top two values from the stack, calculate their sum, and push the result onto the stack.
    SUB: Remove the top two values from the stack, calculate their difference, and push the result onto the stack.
    MUL: Remove the top two values from the stack, calculate their product, and push the result onto the stack.
    DIV: Remove the top two values from the stack, calculate their division, and push the result onto the stack.
    MOD: Remove the top two values from the stack, calculate their remainder, and push the result onto the stack.
    DUP: Copy the top value from the stack and push it onto the stack.
    SWAP: Swap the top two values on the stack.
    JMP <label>: Move program execution to the given label. e.g., JMP LOOP
    JG <label>: If the top value on the stack is greater than 0, move program execution to the given label.
    JL <label>: If the top value on the stack is less than 0, move program execution to the given label.
    JZ <label>: If the top value on the stack is 0, move program execution to the given label.
    JNZ <label>: If the top value on the stack is not 0, move program execution to the given label.
    CMP: Compare the top two values on the stack and push the result onto the stack.
    (-1: First value is smaller, 0: Both values are equal, 1: First value is larger)
    MOV <register>: Move the top value from the stack to the specified register (AX, BX, CX, or DX).
    NOP: Perform no operation. This command is ignored in the program.
    HALT: Stop program execution.
    """

    def __init__(self):
        self.stack = []
        self.program = []
        self.labels = {}
        self.registers = {'AX': 0, 'BX': 0, 'CX': 0, 'DX': 0}
        self.command_list = {}
        self.instruction_pointer = 0
        self._init_command_list()

    def _init_command_list(self):
        self.command_list[Command.PUSH] = self.command_push
        self.command_list[Command.POP] = self.command_pop
        self.command_list[Command.ADD] = self.command_add
        self.command_list[Command.SUB] = self.command_sub
        self.command_list[Command.MUL] = self.command_mul
        self.command_list[Command.DIV] = self.command_div
        self.command_list[Command.MOD] = self.command_mod
        self.command_list[Command.DUP] = self.command_dup
        self.command_list[Command.SWAP] = self.command_swap
        self.command_list[Command.JMP] = self.command_jmp
        self.command_list[Command.JG] = self.command_jg
        self.command_list[Command.JL] = self.command_jl
        self.command_list[Command.JZ] = self.command_jz
        self.command_list[Command.JNZ] = self.command_jnz
        self.command_list[Command.CMP] = self.command_cmp
        self.command_list[Command.MOV] = self.command_mov
        self.command_list[Command.NOP] = self.command_nop
        self.command_list[Command.HALT] = self.command_halt
        self.command_list[Command.LABEL] = self.command_label

    def load_program(self, program):
        self.program = program
        self.labels = {line[:-1]: i for i, line in enumerate(program) if line.endswith(':')}

    def parse_command(self, command_string):
        command, *args = command_string.split()
        if command not in Command.__members__:
            if ':' in command:
                return Command['LABEL'], args
            else:
                raise ValueError(f"Unknown command: {command}")
        return Command[command], args

    def execute_command(self, command, args):
        if command in self.command_list:
            self.command_list[command](args)
        else:
            raise ValueError(f"Unknown command: {command}")

        self.instruction_pointer += 1

    def command_push(self, args):
        value = int(args[0]) if 'X' not in args[0] else self.registers[args[0]]
        self.stack.append(value)

    def command_pop(self, args):
        self.stack.pop()

    def command_add(self, args):
        self.stack.append(self.stack.pop() + self.stack.pop())

    def command_sub(self, args):
        b, a = self.stack.pop(), self.stack.pop()
        self.stack.append(a - b)

    def command_mul(self, args):
        self.stack.append(self.stack.pop() * self.stack.pop())

    def command_div(self, args):
        b, a = self.stack.pop(), self.stack.pop()
        self.stack.append(a // b)

    def command_mod(self, args):
        b, a = self.stack.pop(), self.stack.pop()
        self.stack.append(a % b)

    def command_dup(self, args):
        self.stack.append(self.stack[-1])

    def command_swap(self, args):
        self.stack[-1], self.stack[-2] = self.stack[-2], self.stack[-1]

    def command_jmp(self, args):
        self.instruction_pointer = self.labels[args[0]]

    def command_jg(self, args):
        if self.stack.pop() == 1:
            self.instruction_pointer = self.labels[args[0]]

    def command_jl(self, args):
        if self.stack.pop() == -1:
            self.instruction_pointer = self.labels[args[0]]

    def command_jz(self, args):
        if self.stack.pop() == 0:
            self.instruction_pointer = self.labels[args[0]]

    def command_jnz(self, args):
        if self.stack.pop() != 0:
            self.instruction_pointer = self.labels[args[0]]

    def command_cmp(self, args):
        b, a = self.stack.pop(), self.stack.pop()
        self.stack.append((a > b) - (a < b))

    def command_mov(self, args):
        self.registers[args[0]] = self.stack[-1]

    def command_nop(self, args):
        pass

    def command_halt(self, args):
        pass

    def command_label(self, args):
        pass

    def run(self):
        for _ in range(20000):
            if self.instruction_pointer >= len(self.program):
                break
            command_string = self.program[self.instruction_pointer]
            command, args = self.parse_command(command_string)
            self.execute_command(command, args)

위와 같이 아름답게 코드를 고쳐 주십니다.

그래서 나온 최종 버전은 아래에 있습니다.

https://github.com/chobocho/simple_vm

 

GitHub - chobocho/simple_vm: stack based simple stack machine

stack based simple stack machine. Contribute to chobocho/simple_vm development by creating an account on GitHub.

github.com

 

결론: 

1) 웬만하면 구글링 해서 open source 를 쓰자.

2) ChatGPT-4도 완전한 코드를 짜주지는 않는다 (디버깅은 필수)

Posted by chobocho