{"id":20599359,"url":"https://github.com/xomadev/slime","last_synced_at":"2025-04-15T00:37:53.016Z","repository":{"id":47008242,"uuid":"397319309","full_name":"XomaDev/slime","owner":"XomaDev","description":"Slime is an interpreter made in Java :)","archived":false,"fork":false,"pushed_at":"2021-09-18T12:28:59.000Z","size":167,"stargazers_count":7,"open_issues_count":0,"forks_count":3,"subscribers_count":1,"default_branch":"dev","last_synced_at":"2025-04-15T00:37:47.167Z","etag":null,"topics":["interpreter","java"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/XomaDev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-08-17T16:15:09.000Z","updated_at":"2023-05-07T14:36:19.000Z","dependencies_parsed_at":"2022-09-16T10:22:08.213Z","dependency_job_id":null,"html_url":"https://github.com/XomaDev/slime","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/XomaDev%2Fslime","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/XomaDev%2Fslime/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/XomaDev%2Fslime/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/XomaDev%2Fslime/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/XomaDev","download_url":"https://codeload.github.com/XomaDev/slime/tar.gz/refs/heads/dev","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248986270,"owners_count":21194024,"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":["interpreter","java"],"created_at":"2024-11-16T08:32:41.320Z","updated_at":"2025-04-15T00:37:52.994Z","avatar_url":"https://github.com/XomaDev.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Slime\n\nSlime is a node-based interpreter\n\n```java\n// Create a space to store things\nfinal Space space = new Space();\n\n// Define the slime interpreter\nfinal Slime slime = new Slime(space);\n\n// Execute\nslime.exec(scanner.nextLine());\n\n// Demo tests\n\n// print result of 1 + 1 - (7 + 5)       \nslime.exec(\"print 1 + 1 - (7 + 5)\")\n```\n\n### Process\n\nThe process:\n\n```\nLexing -  Breaking down into tokens\nParsing - Parsed the tokens\nNode Creator - Create a node tree\nNodeReader/processor - Read the nodes and process them into result\n```\n\n### Variable\n\nAsign a value to a variable (`a-zA-Z`) characters are allowed.\n\n```java\na = 'The Android Java';\n```\n\nThe variable can be a number or a string.\n\n```java\n// example inputs\n\na = 70 + (10 + 20)\n        \nprint a is 100 // true\na = 'The number is ' + a\n```\n\n### Print\n\nUse the `print` method to print a variable or a thing.\n\n```java\n// prints value from variable 'a' \n// with value 'The number is 100'\n\nprint a\nprint 50 + (1 + 1) // 52\n        \n// prints 2.1        \nprint .1 + 2\n```\n\n### Basic Operators\n\nSlime can understand `+`, `-`, `/` and `*` math operators, `is` and `or` boolean operator including `braces`.\n\nYou will have to request the interpreter line by line.\n\n\u003chr\u003e\n\n### Custom methods\n\nYou can also declare your methods!\u003cbr\u003e\n\nFor that, you will need to create a class that extends `SlimeMethods`\nand then do change accordingly, an example:\n\n```java\nclass MyMethods extends SlimeMethods {\n    public void trim(final Object text) {\n        System.out.println(String.valueOf(text).trim());\n    }\n}\n```\n\nDeclare a constructor as passing your own SlimeMethods instance as the second parameter.\n\n```java\nfinal Space space = new Space();\nfinal Slime slime = new Slime(space, new MyMethods());\n```\n\n\u003chr\u003e\n\n### Define\n\nYou can also define variable with your own values with slime, you just need to call `define(name, expression)` or `define(name, value)` to define a constant.\n\n```java\nslime.define(\"pie\", valueOf(Math.PI));\nslime.defineConstant(\"cakes\", \"50\")\n```\n\n\u003chr\u003e\n\n### Dynamic operators\n\nYou can also add a dynamic operator, doing additional things!\u003cbr\u003e\nFor that you have to call `setOperator` on `Slime` and pass an operator and the operator handler. This will call the `handle(Object, Object)` method.\u003cbr\u003e\nYou have to perform any operator and return the result.\n\n```java\n// Dynamically adding a boolean operator `\u003c?\u003e is \u003c?\u003e`\nslime.setOperator(\"is\", new Operator() {\n            @Override\n            public Object handle(Object first, Object second) {\n                // checks and returns if first == second\n                return Objects.equals(valueOf(first), valueOf(second));\n            }\n        });\n\nslime.exec(\"a = (70 + (10 + 20)) is 100\")\nslime.exec(\"print a\"); // prints true\n```\n\u003chr\u003e\n\n### Function\n\nYeah! You can also add functions!\u003cbr\u003e\nThere are two inbuilt functions named `max` and `min`.\u003cbr\u003e\nTo access this feature, you will have to use `execBlock` as replacement for `exec` command method. \n\n```java\n// creates a variable named cakeName \n// with the value 'Chocolate Cake!'\n\nslime.execBlock(\"cakeName = 'Chocolate Cake!'\");\n\n// prints 'CHOCOLATE CAKE!'\nslime.execBlock(\"print case(cakeName, 'upper')\");\n\n// prints 'chocolate cake!'\nslime.execBlock(\"print case(cakeName, 'lower')\");\n\n// find the max and  in the args provided\n        \n// prints the number 7     \nslime.execBlock(\"print max(PI, 7)\");\n\n// prints the number 1\n\nslime.execBlock(\"print min(PI, 7, 1)\")\n```\n\nA complicated example to define a function with by calling the method `defineFunction(MethodName, Function)` on Slime object.\n\n```java\ndefineFunction(\"case\", new Function() {\n                @Override\n                public Object handle(ArrayList\u003cObject\u003e parms) {\n                    if (parms.size() != 2)\n                        throw new IllegalArgumentException(\"Expected only two parameter!\");\n                    final Object toCase = parms.get(1);\n                    if (toCase == \"true\" || toCase.equals(\"lower\"))\n                        return valueOf(parms.get(0)).toLowerCase();\n                    else if (toCase == \"false\" || toCase.equals(\"upper\"))\n                        return valueOf(parms.get(0)).toUpperCase();\n                    throw new IllegalArgumentException(\"Not a valid argument '\" + toCase + \"'\");\n                }\n            });\n```\n\u003chr\u003e\nThis was made to learn.\n\u003cbr\u003e\nFeel free to fork and contribute back 🙂\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxomadev%2Fslime","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxomadev%2Fslime","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxomadev%2Fslime/lists"}