minor cleanup

This commit is contained in:
2025-05-22 11:34:29 +03:00
parent 45292990b1
commit 6e9250ffd1
6 changed files with 112 additions and 23 deletions

View File

@@ -6,55 +6,99 @@ import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.function.Supplier; import java.util.function.Supplier;
/**
* The class, used to keep track of important environment-wide values
* Supports an inheritance mechanism via parents
* In essence a simple hashmap of flag objects to values
*/
public class Environment { public class Environment {
/**
* The parent of this environment. Will act as a "fallback" when searching for elements.
* Operations this environment won't write into the parent
*/
public final Environment parent; public final Environment parent;
private final Map<Key<Object>, Object> map = new HashMap<>(); private final Map<Key<Object>, Object> map = new HashMap<>();
private final Set<Key<Object>> hidden = new HashSet<>(); private final Set<Key<Object>> hidden = new HashSet<>();
/**
* Gets the element, contained in this environment, signified by the given key
* @return The element, or null if not found
*/
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> T get(Key<T> key) { public <T> T get(Key<T> key) {
if (map.containsKey(key)) return (T)map.get(key); if (map.containsKey(key)) return (T)map.get(key);
else if (!hidden.contains(key) && parent != null) return parent.get(key); else if (!hidden.contains(key) && parent != null) return parent.get(key);
else return null; else return null;
} }
/**
* Checks if the environment has the given key
*/
public boolean has(Key<?> key) { public boolean has(Key<?> key) {
if (map.containsKey(key)) return true; if (map.containsKey(key)) return true;
else if (!hidden.contains(key) && parent != null) return parent.has(key); else if (!hidden.contains(key) && parent != null) return parent.has(key);
else return false; else return false;
} }
/**
* Checks if the environment has the given key and if the value behind they key is not null
*/
public boolean hasNotNull(Key<?> key) { public boolean hasNotNull(Key<?> key) {
return get(key) != null; return get(key) != null;
} }
/**
* Gets the element, contained in this environment, signified by the given key
* @param defaultVal The value to return if the element is not found
* @return The element, or "defaultVal" if not found
*/
public <T> T get(Key<T> key, T defaultVal) { public <T> T get(Key<T> key, T defaultVal) {
if (has(key)) return get(key); if (has(key)) return get(key);
else return defaultVal; else return defaultVal;
} }
/**
* Gets the element, contained in this environment, signified by the given key
* @param defaultVal The supplier, from which to return if the element is not found
* @return The element, or the result of "defaultVal" if not found
*/
public <T> T getWith(Key<T> key, Supplier<T> defaultVal) { public <T> T getWith(Key<T> key, Supplier<T> defaultVal) {
if (has(key)) return get(key); if (has(key)) return get(key);
else return defaultVal.get(); else return defaultVal.get();
} }
/**
* Inserts the given value for the given key, replacing any existing value
* If a parent has a value with the same key, it isn't replaced, but instead - shadowed
* @return The inserted element
*/
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> Environment add(Key<T> key, T val) { public <T> Environment add(Key<T> key, T val) {
map.put((Key<Object>)key, val); map.put((Key<Object>)key, val);
hidden.remove(key); hidden.remove(key);
return this; return this;
} }
/**
* Adds the flag key to the environment
* @return The environment instance
*/
public Environment add(Key<Void> key) { public Environment add(Key<Void> key) {
return add(key, null); return add(key, null);
} }
/**
* Executes Environment.add for each pair of the map
* @return The environment instance
*/
@SuppressWarnings("all") @SuppressWarnings("all")
public Environment addAll(Map<Key<?>, ?> map, boolean iterableAsMulti) { public Environment addAll(Map<Key<?>, ?> map) {
map.putAll((Map)map); map.putAll((Map)map);
hidden.removeAll(map.keySet()); hidden.removeAll(map.keySet());
return this; return this;
} }
public Environment addAll(Map<Key<?>, ?> map) {
return addAll(map, true);
}
/**
* Removes the given key from the environment.
* If a parent has the given key, it is instead just "hidden" by this environment
* @return The environment instance
*/
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public Environment remove(Key<?> key) { public Environment remove(Key<?> key) {
map.remove(key); map.remove(key);
@@ -62,6 +106,9 @@ public class Environment {
return this; return this;
} }
/**
* If the key exists in the environment, returns it. Otherwise, puts the given value and returns the value
*/
public <T> T init(Key<T> key, T val) { public <T> T init(Key<T> key, T val) {
if (!has(key)) { if (!has(key)) {
this.add(key, val); this.add(key, val);
@@ -69,6 +116,9 @@ public class Environment {
} }
else return get(key); else return get(key);
} }
/**
* If the key exists in the environment, returns it. Otherwise, puts the given value from the supplier and returns it
*/
public <T> T initFrom(Key<T> key, Supplier<T> val) { public <T> T initFrom(Key<T> key, Supplier<T> val) {
if (!has(key)) { if (!has(key)) {
var res = val.get(); var res = val.get();
@@ -78,6 +128,10 @@ public class Environment {
else return get(key); else return get(key);
} }
/**
* Creates an environment that is a child of this environment
* @return
*/
public Environment child() { public Environment child() {
return new Environment(this); return new Environment(this);
} }
@@ -85,15 +139,25 @@ public class Environment {
public Environment(Environment parent) { public Environment(Environment parent) {
this.parent = parent; this.parent = parent;
} }
/**
* Creates an empty environment
*/
public Environment() { public Environment() {
this.parent = null; this.parent = null;
} }
/**
* If the environment is null, returns an empty environment
* Otherwise, returns the given value
*/
public static Environment wrap(Environment env) { public static Environment wrap(Environment env) {
if (env == null) return empty(); if (env == null) return empty();
else return env; else return env;
} }
/**
* Returns a newly-created environment
*/
public static Environment empty() { public static Environment empty() {
return new Environment(); return new Environment();
} }

View File

@@ -2,8 +2,17 @@ package me.topchetoeu.j2s.common;
import java.io.File; import java.io.File;
/**
* The class that represents all filenames in J2S
*/
public class Filename { public class Filename {
/**
* The protocol of the filename (file://, http://, ftp://, etc...)
*/
public final String protocol; public final String protocol;
/**
* The path to the file (/home/me/test.js, example.org/test.js, etc...)
*/
public final String path; public final String path;
@Override public String toString() { @Override public String toString() {
@@ -42,11 +51,18 @@ public class Filename {
this.path = path; this.path = path;
} }
/**
* Parses the given string to a filename.
* If a :// is not found, the protocol will default to "file"
*/
public static Filename parse(String val) { public static Filename parse(String val) {
var i = val.indexOf("://"); var i = val.indexOf("://");
if (i >= 0) return new Filename(val.substring(0, i).trim(), val.substring(i + 3).trim()); if (i >= 0) return new Filename(val.substring(0, i).trim(), val.substring(i + 3).trim());
else return new Filename("file", val.trim()); else return new Filename("file", val.trim());
} }
/**
* Will convert the File instance to a filename, with the protocol set to "file"
*/
public static Filename fromFile(File file) { public static Filename fromFile(File file) {
return new Filename("file", file.getAbsolutePath()); return new Filename("file", file.getAbsolutePath());
} }

View File

@@ -102,16 +102,17 @@ local function parse_impl(str, pos, end_delim)
local delim_found; local delim_found;
if c == "{" then if c == "{" then
pos = pos + 1; pos = str:find("%S", pos + 1) or pos;
local key; local key;
local obj = {}; local obj = {};
c = string.sub(str, pos, pos); c = string.sub(str, pos, pos);
if c == "}" then if c == "}" then
return obj, pos return obj, pos;
else else
while true do while true do
pos = skip_delim(str, pos);
key, pos = parse_str_val(str, pos, true); key, pos = parse_str_val(str, pos, true);
if key == nil then error("Expected a string key") end if key == nil then error("Expected a string key") end

View File

@@ -14,6 +14,9 @@
content: counter(page); content: counter(page);
} }
} }
@page scan-page {
margin: 0;
}
h1 { h1 {
break-before: page; break-before: page;
} }
@@ -196,6 +199,13 @@
break-after: page; break-after: page;
} }
.scan-page {
display: flex;
flex-direction: column;
justify-content: stretch;
align-items: stretch;
page: scan-page;
}
.title-page { .title-page {
display: flex; display: flex;
flex-direction: column; flex-direction: column;
@@ -518,7 +528,15 @@
<div class="title-end">СОФИЯ - {{year}}</div> <div class="title-end">СОФИЯ - {{year}}</div>
</div> </div>
<div class="page asm-page"> <div class="page scan-page">
<img src="./img-secret/scan0001.jpg"/>
</div>
<div class="page scan-page">
<img src="./img-secret/scan0003.jpg"/>
</div>
<!-- <div class="page asm-page">
<div class="school-header"> <div class="school-header">
<img class="school-img" src="{{school_img}}"/> <img class="school-img" src="{{school_img}}"/>
<h4>{{school_name}}</h4> <h4>{{school_name}}</h4>
@@ -561,11 +579,7 @@
</div> </div>
</div> </div>
</div> </div>
</div> </div> -->
<div class="page">
<div>prazna str</div>
</div>
{{content}} {{content}}

View File

@@ -59,9 +59,6 @@ public class Compilers {
public static Compiler transpilerFromSource(Compiler prev, Environment target, Filename compilerName, String compilerSrc) { public static Compiler transpilerFromSource(Compiler prev, Environment target, Filename compilerName, String compilerSrc) {
var env = StdLib.apply(null); var env = StdLib.apply(null);
// var handler = new SimpleDebugHandler();
// env.add(DebugHandler.KEY, handler);
var glob = Value.global(env); var glob = Value.global(env);
var compilerFactory = new FunctionValue[1]; var compilerFactory = new FunctionValue[1];
@@ -86,10 +83,6 @@ public class Compilers {
var compiled = JavaScript.compile(compilerName, compilerSrc, false); var compiled = JavaScript.compile(compilerName, compilerSrc, false);
// for (var el : compiled.all()) {
// handler.onFunctionLoad(el.body(), el.map());
// }
try { try {
new CodeFunction(env, "intializer", compiled.body(), new Value[0][]).apply(env, Value.UNDEFINED); new CodeFunction(env, "intializer", compiled.body(), new Value[0][]).apply(env, Value.UNDEFINED);
return wrap(prev, env, target, compilerFactory[0]); return wrap(prev, env, target, compilerFactory[0]);

View File

@@ -15,8 +15,9 @@ public class Arguments {
public final <T extends Value> T setTargetProto(T obj) { public final <T extends Value> T setTargetProto(T obj) {
if (!self.isPrimitive()) { if (!self.isPrimitive()) {
var proto = self.getMember(env, "prototype"); var proto = self.getMember(env, "prototype");
if (proto instanceof ObjectValue objProto) self.setPrototype(env, objProto);
else if (proto == Value.NULL) self.setPrototype(env, null); if (proto instanceof ObjectValue objProto) obj.setPrototype(env, objProto);
else if (proto == Value.NULL) obj.setPrototype(env, null);
} }
return obj; return obj;
} }