Jim Lang is a programming language based on JVM with a comprehensive language system, aimed at helping everyone get started in the field of language development.
add snapshots repository
<repositories> <repository> <id>jim</id> <url>https://oss.sonatype.org/content/repositories/snapshots</url> </repository> </repositories>set dependency
<dependency> <groupId>com.dafei1288</groupId> <artifactId>jimlang</artifactId> <version>1.0-SNAPSHOT</version> </dependency> @Test public void T3() throws IOException{String script = """ function two(){return 2 } ; function one(){return 1 } ; var x = one() + two() ; println("this message is from jimlang!!!") println( x ) ; """; System.out.println(script); System.out.println("--------------------"); JimLangShell shell = new JimLangShell(); Object ret = shell.eval(script,null)} or use jsr-223
@Test public void test01() throws ScriptException{String script = """ function two(){return 2 } ; function one(){return 1 } ; var x = one() + two() ; println("this message is from jimlang!!!") println( x ) ; """; System.out.println(script); System.out.println("--------------------"); ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("jim"); engine.eval(script)} - Java >= 21
- Maven >= 3.8 (If you want to compile and install IoTDB from source code).
mvn clean package -DskipTests=true
- Language: supports early return from nested blocks (if/while/for)
- Build:
mvn -q -DskipTests package - Run a script:
bin\\jimlang.cmd examples\\fibonacci.jim - Start REPL:
bin\\jimlang.cmd --cli(or-i) - Eval one-liner:
bin\\jimlang.cmd --eval "println(1+2)"(or-e) - Read from STDIN:
echo println(42) | bin\\jimlang.cmd - - Enable trace:
bin\\jimlang.cmd --trace examples\\fibonacci.jim(or setJIM_TRACE=1)
See also:
doc/QUICKREF.mdlanguage snippets, stdlib, CLIdoc/ROADMAP.mdphases, current status, TODO
examples/fibonacci.jimFibonacci sequenceexamples/stdlib_phase3.jimstdlib showcase- Scoping demos:
examples/scope_func.jimfunction-local shadowingexamples/scope_if.jimblock scope shadowingexamples/scope_assign_outer.jimassign to outer var inside function
var o ={a: 1, b: [2,3] } var j = json_encode(o) var x = json_decode(j) println(json_pretty(o, 2)) // file IO json_dump(o, "tmp.json", 2) var ox = json_load("tmp.json") yml_dump(o, "tmp.yml", 2) // var oy = yml_load("tmp.yml") // requires SnakeYAML - Triple-quoted multi-line strings: ''' ... ''' (preserves newlines/whitespace). See doc/QUICKREF.md.\n\n## First-class functions
Note: sysfunctions are also first-class values. You can assign and call them:
var p = println p("ok") function add(a,b){return a + b } var d = add println( d(2, 3) ) - Built-ins: env_get(name[, default]) / env_all() / load_env(path[, override=false])
- Behavior:
- env_get reads from an in-memory overlay (populated via load_env(..., true)) before System.getenv
- env_all returns merged overlay + process envs (overlay wins)
- load_env parses .env (supports leading export, # comments, key=value, strip quotes). If file missing, returns{}. See doc/QUICKREF.md.## Web Server (built-in)
function api(req){return{ok: true } } start_webserver(8080, "/api/ping", "GET", api) - Routes: use triples
(path, method, handler)orroute(path, method, handler)array - Handler receives
req: method, path, params/splat, query, headers, body, json, cookies - Response helpers:
send_text,send_html,send_json,redirect,set_header,response,response_bytes - Files:
send_file,attachment_file; bytes:file_read_bytes
See more: doc/QUICKREF.md (Web section)
- examples/web_app
- examples/env/web_port.jim read .env for PORT and GREETING 锟?complete web app (routes, static, cookies, download)
- Use
JimLangShell.eval(script, sourceName, context)to inject variables:- The entire map is exposed as
ctx(Map). - Identifier-friendly keys (
[A-Za-z_][A-Za-z0-9_]*) are also injected as globals (e.g.,input,discount). - Keys with special characters are accessed by bracket syntax:
ctx["user-id"](currently read-only).
- The entire map is exposed as
Example (Java):
Map<String,Object> input = Map.of( "name", "Alice", "scores", Arrays.asList(2,3,5,7) ); Map<String,Object> ctx = newLinkedHashMap<>(); ctx.put("input", input); ctx.put("discount", 0.85); ctx.put("user-id", "u123"); Stringscript = String.join("\n", "var sum = 0;", "for (var i = 0; i < input.scores.length; i = i + 1){sum = sum + input.scores[i]}", "var uid = ctx[\"user-id\"];", "{name: input.name, final: sum * discount, uid: uid }" ); Objectret = newJimLangShell().eval(script, "<inject-demo>", ctx); System.out.println(ret);- More examples:
examples/DemoEvalInjectCtx.javaexamples/DemoEvalRoundTrip.javaexamples/DemoCallFunctionWithParams.java
Note: ctx["锟斤拷"] is read-only for now; write support (e.g. ctx["k"] = v) can be added later.