{"id":13627861,"url":"https://github.com/forrestchang/gptlang","last_synced_at":"2025-07-12T04:37:15.063Z","repository":{"id":64210557,"uuid":"574073145","full_name":"forrestchang/gptlang","owner":"forrestchang","description":"A new programming language implemented by GPT-4.","archived":false,"fork":false,"pushed_at":"2023-03-15T03:34:08.000Z","size":27,"stargazers_count":345,"open_issues_count":1,"forks_count":13,"subscribers_count":7,"default_branch":"main","last_synced_at":"2025-03-31T16:16:19.416Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/forrestchang.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2022-12-04T10:50:37.000Z","updated_at":"2025-02-23T11:26:16.000Z","dependencies_parsed_at":"2024-01-14T09:19:55.724Z","dependency_job_id":null,"html_url":"https://github.com/forrestchang/gptlang","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/forrestchang%2Fgptlang","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/forrestchang%2Fgptlang/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/forrestchang%2Fgptlang/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/forrestchang%2Fgptlang/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/forrestchang","download_url":"https://codeload.github.com/forrestchang/gptlang/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247713258,"owners_count":20983683,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T22:00:39.491Z","updated_at":"2025-04-07T19:15:40.949Z","avatar_url":"https://github.com/forrestchang.png","language":"Python","funding_links":[],"categories":["NLP","Python","Experiments","SDK, Libraries, Frameworks","Languages","實驗"],"sub_categories":["Other","Python library, sdk or frameworks","Miscellaneous","反向代理網站（Reverse Proxy）"],"readme":"# Introduction\n\nThis repo is an experiment to see if we can create a programming language in GPT-4.\n\nThe original article: https://twitter.com/Tisoga/status/1599347662888882177\n\nGPTLang is a general-purpose, high-level programming language designed for ease of use and readability. It is an interpreted language, meaning that the code is executed directly by the interpreter, without the need for a separate compilation step.\n\nGPTLang has a simple and intuitive syntax that is easy to learn and understand, making it a great language for beginners and experienced programmers alike. It supports a wide range of data types, including numbers, strings, arrays, and user-defined data types.\n\nGPTLang has a rich set of built-in functions and features that make it easy to write powerful and efficient programs. It also has a flexible and extensible architecture that allows users to create their own functions and data types.\n\n![](https://pbs.twimg.com/media/FjIGN4dVQAIuauk?format=jpg\u0026name=large)\n\n# How to use\n\nCopy the raw content of [README.md](https://raw.githubusercontent.com/forrestchang/gptlang/main/README.md) and paste it to ChatGPT. Now you can write GPTLang in ChatGPT.\n\n# Implemention in Python\n\nStill WIP.\n\n👉 [gptlang.py](gptlang.py)\n\n# Examples\n\n## Selection Sort\n\n```gptlang\nFUNC selection_sort(array:arr) -\u003e arr:\n    # Iterate over the elements of the array\n    LOOP array -\u003e elem:\n        # Find the minimum value in the remaining unsorted portion of the array\n        VAR min_index:int = array.index(elem)\n        LOOP array[min_index+1:] -\u003e other_elem:\n            IF other_elem \u003c array[min_index]:\n                min_index = array.index(other_elem)\n\n        # Swap the minimum value with the current element\n        array[array.index(elem)] = array[min_index]\n        array[min_index] = elem\n\n    # Return the sorted array\n    return array\n```\n\n## Quick Sort\n\n```gptlang\n# Define a function named \"quicksort\" that sorts an array of int values using the quicksort algorithm\nFUNC quicksort(nums:arr) -\u003e arr:\n    # If the array has zero or one element, return the array\n    IF LEN(nums) \u003c= 1:\n        return nums\n\n    # Initialize the pivot variable to the first element of the array\n    VAR pivot:int nums[0]\n\n    # Initialize the left and right arrays to empty arrays\n    VAR left:arr []\n    VAR right:arr []\n\n    # Iterate over the elements of the array\n    LOOP nums[1:] -\u003e elem:\n        # If the current element is less than the pivot, append it to the left array,\n        # otherwise append it to the right array\n        IF elem \u003c pivot:\n            left.append(elem)\n        ELSE:\n            right.append(elem)\n\n    # Recursively sort the left and right arrays\n    left = quicksort(left)\n    right = quicksort(right)\n\n    # Return the concatenation of the left array, the pivot, and the right array\n    return left + [pivot] + right\n```\n\nThis function takes an array of int values as an argument and returns the sorted array. It uses the quicksort algorithm to sort the array by selecting a pivot element, partitioning the array into elements that are less than and greater than the pivot, and then recursively sorting the left and right partitions.\n\nFor example, calling quicksort([5, 8, 6, 3, 9, 1]) would return [1, 3, 5, 6, 8, 9].\n\n## Knuth-Morris-Pratt (KMP) Algorithm\n\n```gptlang\nFUNC kmp_search(text:str, pattern:str) -\u003e arr:\n    # Generate the longest proper prefix-suffix array (LPS array) for the pattern\n    VAR lps:arr = generate_lps(pattern)\n\n    VAR i:int = 0  # Index for text\n    VAR j:int = 0  # Index for pattern\n\n    VAR results:arr = []\n\n    WHILE i \u003c LEN(text):\n        IF text[i] == pattern[j]:\n            i += 1\n            j += 1\n        ELSE:\n            IF j != 0:\n                j = lps[j - 1]\n            ELSE:\n                i += 1\n\n        # If the pattern is found in the text, add the starting index to the results array\n        IF j == LEN(pattern):\n            results.append(i - j)\n            j = lps[j - 1]\n\n    return results\n\nFUNC generate_lps(pattern:str) -\u003e arr:\n    VAR lps:arr = [0] * LEN(pattern)\n    VAR len:int = 0  # Length of the previous longest proper prefix-suffix\n    VAR i:int = 1\n\n    WHILE i \u003c LEN(pattern):\n        IF pattern[i] == pattern[len]:\n            len += 1\n            lps[i] = len\n            i += 1\n        ELSE:\n            IF len != 0:\n                len = lps[len - 1]\n            ELSE:\n                lps[i] = 0\n                i += 1\n\n    return lps\n\n\nVAR text:str = \"ABABDABACDABABCABAB\"\nVAR pattern:str = \"ABABCABAB\"\n\nVAR result:arr = kmp_search(text, pattern)\nPT(result)  # Output: [10], as the pattern starts at index 10 in the text\n```\n\n# Basic Syntax\n\n## 1. \u003ca name='Comments'\u003e\u003c/a\u003eComments\n\nA comment in GPTLang starts with a hash character (#) and extends to the end of the physical line. A comment signifies the end of the logical line unless the implicit line joining rules are invoked. Comments are ignored by the syntax and are used to provide explanatory notes and documentation for the code.\n\nHere is an example of a comment in GPTLang:\n\n```gptlang\n# This is a comment\n```\n\n## 2. \u003ca name='Variables'\u003e\u003c/a\u003eVariables\n\nA variable in GPTLang is a named storage location that holds a value. A variable must be declared with a VAR keyword before it can be used in the code. The syntax for declaring a variable is:\n\n```gptlang\nVAR \u003cvariable_name\u003e:\u003cdata_type\u003e \u003cvalue\u003e\n```\n\nThe \u003cvariable_name\u003e field specifies the name of the variable. The \u003cdata_type\u003e field specifies the data type of the variable. The \u003cvalue\u003e field specifies the initial value of the variable.\n\nHere are some examples of variable declarations in GPTLang:\n\n```gptlang\n# Declare a string variable named \"message\" with the initial value \"Hello, world!\"\nVAR message:str \"Hello, world!\"\n\n# Declare an integer variable named \"count\" with the initial value 0\nVAR count:int 0\n\n# Declare a float variable named \"pi\" with the initial value 3.14\nVAR pi:float 3.14\n```\n\n## 3. \u003ca name='Functions'\u003e\u003c/a\u003eFunctions\n\nA function in GPTLang is a self-contained block of code that performs a specific task and optionally returns a value. A function must be defined with a FUNC keyword before it can be called in the code. The syntax for defining a function is:\n\n```gptlang\nFUNC \u003cfunction_name\u003e(\u003carguments\u003e) -\u003e \u003creturn_values\u003e:\n    \u003cfunction_body\u003e\n```\n\nThe \u003cfunction_name\u003e field specifies the name of the function. The \u003carguments\u003e field specifies the input parameters of the function. The \u003creturn_values\u003e field specifies the data type of the value returned by the function. The \u003cfunction_body\u003e field contains the code that makes up the body of the function.\n\nHere is an example of a function definition in GPTLang:\n\n```gptlang\n# Define a function named \"add\" that takes two integer arguments and returns an integer value\nFUNC add(a:int, b:int) -\u003e int:\n    # Calculate the sum of the two arguments\n    VAR result:int = a + b\n\n    # Return the result\n    return result\n```\n\n## 4. \u003ca name='ConditionalExecution'\u003e\u003c/a\u003eConditional Execution\n\nGPTLang supports conditional execution using the IF and ELSE keywords. The syntax for conditional execution is:\n\n```gptlang\nIF \u003ccondition\u003e:\n    \u003cstatements\u003e\nELSE:\n    \u003cstatements\u003e\n```\n\nThe \u003ccondition\u003e field specifies a Boolean expression that determines whether the \u003cstatements\u003e in the IF clause or the ELSE clause are executed. If the \u003ccondition\u003e is true, the \u003cstatements\u003e in the IF clause are executed. If the \u003ccondition\u003e is false and an ELSE clause is present, the \u003cstatements\u003e in the ELSE clause are executed.\n\nHere is an example of conditional execution in GPTLang:\n\n```gptlang\n# Define a variable named \"num\" with the initial value 10\nVAR num:int 10\n\n# Check if the value of \"num\" is less than 8\nIF num \u003c 8:\n    # If the value of \"num\" is less than 8, print \"Ping\"\n    PT(\"Ping\")\nELSE:\n    # If the value of \"num\" is greater than or equal to 8, print \"Pong\"\n    PT(\"Pong\")\n```\n\nIn this example, the IF clause will be executed and the string \"Ping\" will be printed to the screen, because the value of the num variable is 10, which is greater than 8.\n\n## 5. \u003ca name='Loops'\u003e\u003c/a\u003eLoops\n\nGPTLang supports looping using the LOOP keyword. The syntax for looping is:\n\n```gptlang\nLOOP \u003citerable_object\u003e -\u003e \u003celement\u003e:\n    \u003cstatements\u003e\n```\n\nThe \u003citerable_object\u003e field specifies an object that can be iterated over, such as a string, tuple, or list. The \u003celement\u003e field specifies a variable that will be assigned the value of each element in the \u003citerable_object\u003e as the loop progresses. The \u003cstatements\u003e field contains the code that is executed for each iteration of the loop.\n\nHere is an example of looping in GPTLang:\n\n```gptlang\n# Define an array named \"numbers\" with the initial values [5, 8, 6, 3, 9, 1]\nVAR numbers:arr [5, 8, 6, 3, 9, 1]\n\n# Loop over the elements in the \"numbers\" array\nLOOP numbers -\u003e num:\n    # Print the value of each element in the \"numbers\" array\n    PT(num)\n```\n\nIn this example, the LOOP statement will iterate over the elements in the numbers array, and the value of each element will be assigned to the num variable in turn. For each iteration, the PT(num) statement will be executed, which will print the value of the num variable to the screen.\n\n## 6. \u003ca name='Arrays'\u003e\u003c/a\u003eArrays\n\nGPTLang supports arrays, which are ordered collections of elements of the same data type. An array can be declared with the VAR keyword and the arr data type. The syntax for declaring an array is:\n\n```gptlang\nVAR \u003carray_name\u003e:arr [\u003celement1\u003e, \u003celement2\u003e, ...]\n```\n\nThe \u003carray_name\u003e field specifies the name of the array. The \u003celement1\u003e, \u003celement2\u003e, ... fields specify the initial values of the array elements.\n\nHere is an example of declaring an array in GPTLang:\n\n```gptlang\n# Declare an array named \"numbers\" with the initial values [5, 8, 6, 3, 9, 1]\nVAR numbers:arr [5, 8, 6, 3, 9, 1]\n```\n\nAn array can be iterated over using the LOOP statement, as shown in the previous section. The length of an array can be obtained using the LEN() function. Elements of an array can be accessed using their index, which is the position of the element in the array, starting at 0. The syntax for accessing an array element is:\n\n```gptlang\n\u003carray_name\u003e[\u003cindex\u003e]\n```\n\nHere is an example of accessing an array element in GPTLang:\n\n```\n# Define an array named \"numbers\" with the initial values [5, 8, 6, 3, 9, 1]\nVAR numbers:arr [5, 8, 6, 3, 9, 1]\n\n# Print the second element of the \"numbers\" array\nPT(numbers[1])\n```\n\nIn this example, the PT(numbers[1]) statement will print the value of the second element in the numbers array, which is 8, to the screen.\n\n## 7. \u003ca name='User-DefinedDataTypes'\u003e\u003c/a\u003eUser-Defined Data Types\n\nGPTLang supports user-defined data types, which allow users to create their own custom data structures. A user-defined data type can be declared with the TYPE keyword. The syntax for declaring a user-defined data type is:\n\n```gptlang\nTYPE \u003ctype_name\u003e(\u003cfield1\u003e:\u003cdata_type1\u003e, \u003cfield2\u003e:\u003cdata_type2\u003e, ...):\n    \u003cfield_initialization\u003e\n```\n\nThe \u003ctype_name\u003e field specifies the name of the user-defined data type. The \u003cfield1\u003e, \u003cfield2\u003e, ... fields specify the fields of the data type, and the \u003cdata_type1\u003e, \u003cdata_type2\u003e, ... fields specify the data type of each field. The \u003cfield_initialization\u003e field specifies the initial values of the fields of the data type.\n\nHere is an example of declaring a user-defined data type in GPTLang:\n\n```\n# Define a user-defined data type named \"point\" with fields \"x\" and \"y\" of type int\nTYPE point(x:int, y:int):\n    # Initialize the fields of the \"point\" data type\n    x = 0\n    y = 0\n```\n\nOnce a user-defined data type has been declared, it can be used to create variables of that data type. The syntax for declaring a variable of a user-defined data type is:\n\n```\nVAR \u003cvariable_name\u003e:\u003ctype_name\u003e\n```\n\nThe \u003cvariable_name\u003e field specifies the name of the variable, and the \u003ctype_name\u003e field specifies the name of the user-defined data type.\n\nHere is an example of declaring a variable of a user-defined data type in GPTLang:\n\n```\n# Define a user-defined data type named \"point\" with fields \"x\" and \"y\" of type int\nTYPE point(x:int, y:int):\n    # Initialize the fields of the \"point\" data type\n    x = 0\n    y = 0\n\n# Declare a variable named \"origin\" of type \"point\"\nVAR origin:point\n```\n\nIn this example, the origin variable will be of type point, and it will have the fields x and y with initial values of 0.\n\n# Built-in Functions and Features\n\nGPTLang has a rich set of built-in functions and features that make it easy to write powerful and efficient programs. Some of the notable built-in functions and features of GPTLang are described below.\n\n## 8. \u003ca name='PTFunction'\u003e\u003c/a\u003ePT() Function\n\nThe PT() function is a built-in function that prints the specified value to the screen. The syntax for the PT() function is:\n\n```\nPT(\u003cvalue\u003e) -\u003e None\n```\n\nThe \u003cvalue\u003e field specifies the value to be printed. The PT() function does not return any value.\n\nHere is an example of using the PT() function in GPTLang:\n\n```\n# Define a variable named \"message\" with the initial value \"Hello, world!\"\nVAR message:str \"Hello, world!\"\n\n# Print the value of the \"message\" variable to the screen\nPT(message)\n```\n\nIn this example, the PT(message) statement will print the value of the message variable, which is \"Hello, world!\", to the screen.\n\n## 9. \u003ca name='LENFunction'\u003e\u003c/a\u003eLEN() Function\n\nThe LEN() function is a built-in function that returns the length of a specified value. The syntax for the LEN() function is:\n\n```\nLEN(\u003cvalue\u003e) -\u003e int\n```\n\nThe \u003cvalue\u003e field specifies the value for which the length is to be calculated. The LEN() function returns an integer value representing the length of the specified value.\n\nHere is an example of using the LEN() function in GPTLang:\n\n```\n# Define an array named \"numbers\" with the initial values [5, 8, 6, 3, 9, 1]\nVAR numbers:arr [5, 8, 6, 3, 9, 1]\n\n# Print the length of the \"numbers\" array to the screen\nPT(LEN(numbers))\n```\n\nIn this example, the PT(LEN(numbers)) statement will print the length of the numbers array, which is 6, to the screen.\n\n## Implicit Line Joining\n\nGPTLang supports implicit line joining, which allows multiple physical lines of code to be treated as a single logical line. This can be useful for making long lines of code more readable by breaking them up into multiple physical lines.\n\nImplicit line joining is performed by ending a physical line with a backslash (\\) character. The next physical line will be treated as part of the same logical line. This continues until a physical line is encountered that does not end with a backslash character.\n\nHere is an example of implicit line joining in GPTLang:\n\n```\n# Define a variable named \"message\" with the initial value \"Hello, world!\\\n# This is a long message that spans multiple physical lines.\"\nVAR message:str \"Hello, world!\\\nThis is a long message that spans multiple physical lines.\"\n\n# Print the value of the \"message\" variable to the screen\nPT(message)\n```\n\nIn this example, the VAR statement defining the message variable uses implicit line joining. The message variable will have the initial value \"Hello, world! This is a long message that spans multiple physical lines.\", which is the concatenation of the two physical lines on which the VAR statement is written.\n\nThe PT(message) statement will print the value of the message variable to the screen, which is \"Hello, world! This is a long message that spans multiple physical lines.\"\n\n## Exception Handling\n\nGPTLang supports exception handling, which allows developers to handle runtime errors in their programs in a controlled and predictable way. Exception handling is performed using the TRY and EXCEPT keywords.\n\nThe syntax for exception handling in GPTLang is:\n\n```\nTRY:\n    \u003cstatements\u003e\nEXCEPT \u003cexception_type\u003e as \u003cvariable_name\u003e:\n    \u003cstatements\u003e\n```\n\nThe TRY keyword indicates the start of a block of code that is to be executed and monitored for exceptions. The EXCEPT keyword indicates a block of code that is to be executed if an exception of the specified type is raised in the TRY block. The \u003cexception_type\u003e field specifies the type of the exception to be handled, and the \u003cvariable_name\u003e field specifies the name of the variable that will contain the exception object.\n\nHere is an example of exception handling in GPTLang:\n\n```\n# Define a function named \"divide\" that takes two int arguments and returns their quotient\nFUNC divide(a:int, b:int) -\u003e int:\n    TRY:\n        # Divide the first argument by the second argument and return the result\n        return a / b\n    EXCEPT ZeroDivisionError as e:\n        # If a ZeroDivisionError is raised, print an error message and return 0\n        PT(\"Error: Cannot divide by zero\")\n        return 0\n```\n\nIn this example, the divide() function is defined. It takes two int arguments, a and b, and returns their quotient. The function contains a TRY block that divides a by b and returns the result. It also contains an EXCEPT block that catches ZeroDivisionError exceptions and prints an error message if one is raised.\n\nIf the divide() function is called with a=5 and b=0, a ZeroDivisionError exception will be raised in the TRY block. This exception will be caught by the EXCEPT block, which will print an error message and return 0.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fforrestchang%2Fgptlang","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fforrestchang%2Fgptlang","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fforrestchang%2Fgptlang/lists"}