Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

一种基于拼音输入法的编程语言转译方案 #93

Closed
4b5ent1 opened this issue Oct 16, 2018 · 7 comments
Closed

一种基于拼音输入法的编程语言转译方案 #93

4b5ent1 opened this issue Oct 16, 2018 · 7 comments
Labels
APL +组合逻辑/tacit族 设计 design
Milestone

Comments

@4b5ent1
Copy link
Member

4b5ent1 commented Oct 16, 2018

主要解决切换输入法的问题。
原则:

  1. 保留原有的抽象结构
  2. 不全盘汉化。如function这种可以所写成fn
  3. 确保视觉可读性的同时,尽可能缩短代码
@4b5ent1
Copy link
Member Author

4b5ent1 commented Oct 16, 2018

例1/from typescript:playground https://www.typescriptlang.org/play/index.html

class Greeter {
    greeting: string;
    constructor(message: string) {
        this.greeting = message;
    }
    greet() {
        return "Hello, " + this.greeting;
    }
}

let greeter = new Greeter("world");

let button = document.createElement('button');
button.textContent = "Say Hello";
button.onclick = function() {
    alert(greeter.greet());
}

document.body.appendChild(button);
类 G【
greeting/字;
=“msg:字”【 此·greeting=msg;】
greet“”【= {hello,}+此·greeting 】
】

令 g = + G“{world}”
令 按钮 = 文档·createElment“{button}”
按钮·内容= {say hello}
按钮·单击 = 。【提示“g·greet。 ”】

文档·体·appendchild“按钮”

这里用的符号集,是默认微软拼音输入法的,中文/半角/中文标点

@4b5ent1
Copy link
Member Author

4b5ent1 commented Oct 16, 2018

上面这个例1,为了尽量避免shift键

“”替代了(),同时也可以用。替代()

/和:可以通用

@4b5ent1
Copy link
Member Author

4b5ent1 commented Oct 16, 2018

检测缩进时,只需要检测是否存在空格(不少于1个即可)

@4b5ent1
Copy link
Member Author

4b5ent1 commented Oct 16, 2018

例2: from ts/playground

class Animal {
    constructor(public name: string) { }
    move(distanceInMeters: number = 0) {
        console.log(`${this.name} moved ${distanceInMeters}m.`);
    }
}

class Snake extends Animal {
    constructor(name: string) { super(name); }
    move(distanceInMeters = 5) {
        console.log("Slithering...");
        super.move(distanceInMeters);
    }
}

class Horse extends Animal {
    constructor(name: string) { super(name); }
    move(distanceInMeters = 45) {
        console.log("Galloping...");
        super.move(distanceInMeters);
    }
}

let sam = new Snake("Sammy the Python");
let tom: Animal = new Horse("Tommy the Palomino");

sam.move();
tom.move(34);

@4b5ent1
Copy link
Member Author

4b5ent1 commented Oct 16, 2018

类 动物(
 =[^名字/字]()
 移动[距离/数=0](它=此`名字,多少=距离,
  控`记录[\{它}移动了{多少}米\))

类 蛇 从 动物(
 =[名/字](传[名])
 爬[米=5](控`印[\ssss\]
  从`移动[米]))

类 马 从 动物(
 =[名/字](传[名])
 行[路程=45](控`印[\马叫声\]
  从`移动[路程]))

令 s=+ 蛇[\python\]
令 t/动物=+ 马[\tom\]
s`爬。
t`行[34]

这里用的符号集,是全角+英文标点。从=extends,行=implements,形=interface,型=type

@4b5ent1 4b5ent1 added 设计 design APL +组合逻辑/tacit族 labels Oct 16, 2018
@4b5ent1
Copy link
Member Author

4b5ent1 commented Oct 16, 2018

例三:
P=public,#=private,S=static
[大写]P[空格] = [全角大写]P

类 v{
 =(Px/数,Py/数,Pz/数){}
 S乘(k/数,i/v){=+v(k*i·x,i·y,i·z)}
 S加(a/v,b/v){=+v/a·x+b·x,a·y+b·y,a·z+b·z }
 S减(a/v,b/v){=+v/a·x-b·x,a·y-b·y,a·z-b·z }
 S点(a/v,b/v){=a·x*b·x,a·y*b·y,a·z*b·z }
 Sm(a/v){;mag,f=(x)》x*x
  =Math·sqrt(f/a·x+f/a·y+f/a·z) }
 Sn(a/v){;normal,
  令 m=a·m。
  令 除=(m为0)?无穷/1.0%m
  =v·乘(除,a)}
 S叉(a/v,b/v){
  =+v(a·y*b·z-a·z*b·y,
      a·z*b·x-a·x*b·z,
      a·x*b·y-a·y*b·x)}}

类 色{
}
# 还没写完
class Color {

    constructor(public r: number, public g: number, public b: number) { }

    static scale(k: number, v: Color) {
        return new Color(k * v.r, k * v.g, k * v.b);
    }

    static plus(v1: Color, v2: Color) {
        return new Color(v1.r + v2.r, v1.g + v2.g, v1.b + v2.b);
    }

    static times(v1: Color, v2: Color) {
        return new Color(v1.r * v2.r, v1.g * v2.g, v1.b * v2.b);
    }

    static white = new Color(1.0, 1.0, 1.0);
    static grey = new Color(0.5, 0.5, 0.5);
    static black = new Color(0.0, 0.0, 0.0);
    static background = Color.black;
    static defaultColor = Color.black;

    static toDrawingColor(c: Color) {
        let legalize = d => d > 1 ? 1 : d;
        return {
            r: Math.floor(legalize(c.r) * 255),
            g: Math.floor(legalize(c.g) * 255),
            b: Math.floor(legalize(c.b) * 255)
        }
    }

}

class Camera {

    forward: Vector;
    right: Vector;
    up: Vector;

    constructor(public pos: Vector, lookAt: Vector) {
        let down = new Vector(0.0, -1.0, 0.0);
        this.forward = Vector.norm(Vector.minus(lookAt, this.pos));
        this.right = Vector.times(1.5, Vector.norm(Vector.cross(this.forward, down)));
        this.up = Vector.times(1.5, Vector.norm(Vector.cross(this.forward, this.right)));
    }

}

interface Ray {
    start: Vector;
    dir: Vector;
}

interface Intersection {
    thing: Thing;
    ray: Ray;
    dist: number;
}

interface Surface {
    diffuse: (pos: Vector) => Color;
    specular: (pos: Vector) => Color;
    reflect: (pos: Vector) => number;
    roughness: number;
}

interface Thing {
    intersect: (ray: Ray) => Intersection;
    normal: (pos: Vector) => Vector;
    surface: Surface;
}

interface Light {
    pos: Vector;
    color: Color;
}

interface Scene {
    things: Thing[];
    lights: Light[];
    camera: Camera;
}

class Sphere implements Thing {

    radius2: number;

    constructor(public center: Vector, radius: number, public surface: Surface) {
        this.radius2 = radius * radius;
    }

    normal(pos: Vector): Vector {
        return Vector.norm(Vector.minus(pos, this.center));
    }

    intersect(ray: Ray) {
        let eo = Vector.minus(this.center, ray.start);
        let v = Vector.dot(eo, ray.dir);
        let dist = 0;
        if (v >= 0) {
            let disc = this.radius2 - (Vector.dot(eo, eo) - v * v);
            if (disc >= 0) {
                dist = v - Math.sqrt(disc);
            }
        }
        if (dist === 0) {
            return null;
        } else {
            return { thing: this, ray: ray, dist: dist };
        }
    }

}

class Plane implements Thing {

    normal: (pos: Vector) => Vector;
    intersect: (ray: Ray) => Intersection;

    constructor(norm: Vector, offset: number, public surface: Surface) {
        this.normal = function(pos: Vector) { return norm; }
        this.intersect = function(ray: Ray): Intersection {
            let denom = Vector.dot(norm, ray.dir);

            if (denom > 0) {
                return null;
            }
            else {
                let dist = (Vector.dot(norm, ray.start) + offset) / (-denom);
                return { thing: this, ray: ray, dist: dist };
            }
        }
    }

}

namespace Surfaces {

    export let shiny: Surface = {
        diffuse: function(pos) { return Color.white; },
        specular: function(pos) { return Color.grey; },
        reflect: function(pos) { return 0.7; },
        roughness: 250
    }

    export let checkerboard: Surface = {
        diffuse: function(pos) {
            if ((Math.floor(pos.z) + Math.floor(pos.x)) % 2 !== 0) {
                return Color.white;
            } else {
                return Color.black;
            }
        },
        specular: function(pos) { return Color.white; },
        reflect: function(pos) {
            if ((Math.floor(pos.z) + Math.floor(pos.x)) % 2 !== 0) {
                return 0.1;
            } else {
                return 0.7;
            }
        },
        roughness: 150
    }

}


class RayTracer {

    private maxDepth = 5;

    private intersections(ray: Ray, scene: Scene) {
        let closest = +Infinity;
        let closestInter: Intersection = undefined;
        for (let i in scene.things) {
            let inter = scene.things[i].intersect(ray);
            if (inter != null && inter.dist < closest) {
                closestInter = inter;
                closest = inter.dist;
            }
        }
        return closestInter;
    }

    private testRay(ray: Ray, scene: Scene) {
        let isect = this.intersections(ray, scene);
        if (isect != null) {
            return isect.dist;
        } else {
            return undefined;
        }
    }

    private traceRay(ray: Ray, scene: Scene, depth: number): Color {
        let isect = this.intersections(ray, scene);
        if (isect === undefined) {
            return Color.background;
        } else {
            return this.shade(isect, scene, depth);
        }
    }

    private shade(isect: Intersection, scene: Scene, depth: number) {
        let d = isect.ray.dir;
        let pos = Vector.plus(Vector.times(isect.dist, d), isect.ray.start);
        let normal = isect.thing.normal(pos);
        let reflectDir = Vector.minus(d, Vector.times(2, Vector.times(Vector.dot(normal, d), normal)));
        let naturalColor = Color.plus(Color.background,
                                      this.getNaturalColor(isect.thing, pos, normal, reflectDir, scene));
        let reflectedColor = (depth >= this.maxDepth) ? Color.grey : this.getReflectionColor(isect.thing, pos, normal, reflectDir, scene, depth);
        return Color.plus(naturalColor, reflectedColor);
    }

    private getReflectionColor(thing: Thing, pos: Vector, normal: Vector, rd: Vector, scene: Scene, depth: number) {
        return Color.scale(thing.surface.reflect(pos), this.traceRay({ start: pos, dir: rd }, scene, depth + 1));
    }

    private getNaturalColor(thing: Thing, pos: Vector, norm: Vector, rd: Vector, scene: Scene) {
        let addLight = (col, light) => {
            let ldis = Vector.minus(light.pos, pos);
            let livec = Vector.norm(ldis);
            let neatIsect = this.testRay({ start: pos, dir: livec }, scene);
            let isInShadow = (neatIsect === undefined) ? false : (neatIsect <= Vector.mag(ldis));
            if (isInShadow) {
                return col;
            }
            else {
                let illum = Vector.dot(livec, norm);
                let lcolor = (illum > 0) ? Color.scale(illum, light.color)
                                          : Color.defaultColor;
                let specular = Vector.dot(livec, Vector.norm(rd));
                let scolor = (specular > 0) ? Color.scale(Math.pow(specular, thing.surface.roughness), light.color)
                                          : Color.defaultColor;
                return Color.plus(col, Color.plus(Color.times(thing.surface.diffuse(pos), lcolor),
                                                  Color.times(thing.surface.specular(pos), scolor)));
            }
        }
        return scene.lights.reduce(addLight, Color.defaultColor);
    }

    render(scene, ctx, screenWidth, screenHeight) {
        let getPoint = (x, y, camera) => {
            let recenterX = x => (x - (screenWidth / 2.0)) / 2.0 / screenWidth;
            let recenterY = y => -(y - (screenHeight / 2.0)) / 2.0 / screenHeight;
            return Vector.norm(Vector.plus(camera.forward, Vector.plus(Vector.times(recenterX(x), camera.right), Vector.times(recenterY(y), camera.up))));
        }
        for (let y = 0; y < screenHeight; y++) {
            for (let x = 0; x < screenWidth; x++) {
                let color = this.traceRay({ start: scene.camera.pos, dir: getPoint(x, y, scene.camera) }, scene, 0);
                let c = Color.toDrawingColor(color);
                ctx.fillStyle = "rgb(" + String(c.r) + ", " + String(c.g) + ", " + String(c.b) + ")";
                ctx.fillRect(x, y, x + 1, y + 1);
            }
        }
    }

}


function defaultScene(): Scene {
    return {
        things: [new Plane(new Vector(0.0, 1.0, 0.0), 0.0, Surfaces.checkerboard),
                 new Sphere(new Vector(0.0, 1.0, -0.25), 1.0, Surfaces.shiny),
                 new Sphere(new Vector(-1.0, 0.5, 1.5), 0.5, Surfaces.shiny)],
        lights: [{ pos: new Vector(-2.0, 2.5, 0.0), color: new Color(0.49, 0.07, 0.07) },
                 { pos: new Vector(1.5, 2.5, 1.5), color: new Color(0.07, 0.07, 0.49) },
                 { pos: new Vector(1.5, 2.5, -1.5), color: new Color(0.07, 0.49, 0.071) },
                 { pos: new Vector(0.0, 3.5, 0.0), color: new Color(0.21, 0.21, 0.35) }],
        camera: new Camera(new Vector(3.0, 2.0, 4.0), new Vector(-1.0, 0.5, 0.0))
    };
}

function exec() {
    let canv = document.createElement("canvas");
    canv.width = 256;
    canv.height = 256;
    document.body.appendChild(canv);
    let ctx = canv.getContext("2d");
    let rayTracer = new RayTracer();
    return rayTracer.render(defaultScene(), ctx, 256, 256);
}

exec();

@4b5ent1
Copy link
Member Author

4b5ent1 commented Oct 16, 2018

这个方案先close了,转到#94去。
#94

@4b5ent1 4b5ent1 closed this as completed Oct 16, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
APL +组合逻辑/tacit族 设计 design
Projects
None yet
Development

No branches or pull requests

1 participant