Quantcast
Channel: C#タグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 9743

新言語シールド言語フレームワークの提案

$
0
0

新言語の提案

新言語としてシールド言語(shield lang)を提案します。
1.シールド言語の特徴
GUIや3DCGや画像処理のフレームワークがとても揃っている。動的型作成可能な静的型付けの言語(Jitコンパイラ型(インタプリタ併用)と事前コンパイル型(.jcp(Jit Compiled Program の略)ファイル生成(VMで起動できる形式)上記にはGCが存在する。,exe等作成(ポインタやRustのようなスコープが存在するので学習コストがとても高いがC++並みの速度が出せる(動的型作成不可))の3つある。学習コストが低いが、exe等にするとJava並み。文法はC#とPythonとKotlinに似ている。
2.シールド言語の文法

ex.shie
#これはコメントです。
(*これもコメントです。*)
#コンパイラ付属 using ~ as *で名前空間を省略可
using system #exit(プログラム終了),TapKey(キー入力)など
using os #id(アドレスの出力),exit(終了) ptr(ポインタ操作)など
using rapt #標準便利機能
using console as * #print,read,errorなど 
using err # comment,debugなど
#ファイル操作はusing file

 #外部ファイルはimport 今回はOpenCVをimportしている。
import cv

rect = rapt.rect #namespace rapt class rectをrectにしている
using texisnone as rapt.texture.isnone #def rapt.textureisnone(bool)()を別名を与えている。
var n1 = 5
var s1 = "string1"
dynamic d = 5 #動的型
d = "あいう"
#代入は"=",比較は"=="
str s2 = "string2"
opt str s3 = None #参照型はNone
opt float n2 = Null #値型はNull
var list[] = new {1,"ABC",3.141592} #listは参照型かつobj派生型一度型が決まれば変わらない。
list[1] = 3 #コンパイルエラー
var arr[3] = Array<str>{"1","ABC","3.141592"} #配列は値型
var dic[] = new dict<str,int>{["あいう":1],["うえお":2]}
var tupple[] = new [1,"ABC",3.14159265358979]
#constだと定数宣言が可能。
const Anum = 5 
var Aiu[Anum] = Arrey<int>{1,2,3,4,5}
tupple[2] =3.14 #コンパイルエラー

if(type(n1) is int or opt int):#true
    print(f"{n1}")

s1 = switch(s1) case "2" case "3" as "s1は"+s1+"です" break case "string1" case "string2" default as "s1は2ではありません" break
print(f"{s1}") #output "s1は2ではありません"

enum VakKey:#ほんの一部
    LEFT = 0x25
    RIGHT
    UP 
    DOWN
    a = 0x41
    b
    c
    d

var vk[] = new VakKey()<-すべての状態を知る箱を作成

with val as vk: #返り値ではなくswitchのカッコ内がtrueかを知りたい場合with val as ~構文を使う。この時はcontinueを使う。
    switch(system.TapKey(int(val)):
        case vk.RIGHT as #以下が実行されます。
        print("Right")
        continue
        case vk.LEFT as
        print("Left")
        continue
        case vk.UP as
        print("Up")
        continue
        case vk.DOWN as
        print("Down")
        continue

struct tanf: #構造体は値型

    pub unsafe def _init(int m,str w,var x) without this: #コンストラクタは戻り値不要
        .m = m #without内では.~で書くことができる。
        .w = w
        .x = global s1 #グローバル変数やclass外変数、ポインタを使うときunsafeを使う。
        return this

    pub def show()():#()は戻り値の型voidの時は省略可能return None
        read (f"{this.m}")
        for(i in range(this.m)):
            print(str(i)+"回目")
            print(f"{this.w}が{this.x}です")
            pass

    set(val) get int m 
    # pub def set_m(int m){this.m = val} と pub def get_m(){return this.m}が自動作成される
    pub str w 
    set(val) get del var x
    # pub def del_x(){del this.x}が自動作成される 

dat class Mycdat(obj)(opt str mydat1,opt int mydat2)#(継承/aspect)(データ)
dat struct Mysdat(opt str mydat1,opt int mydat2)

class Myclass(obj):#(継承/aspect)クラスは参照型

    pub def _init(int m,ref rect rc,var h,var y,var t[931]):
        this.m = m
        this.rc = rc
        this.h = h
        this.y = y
        this.t = [g for i in range(931) g = if(t[i] is Null) 5 else t[i]]

    pub def show()(int m = 3):
        this.m = m
        var n = 0
        for(not n is this.m and rc is not None):# 
            print(rc.left,rc.right,rc.top,rc.bottom)
        var rect_is_live = if(m < 3) true elif(6 < m and m < 10) false else true
        this.rc = if(not rect_is_live) None else pass

    pub def show()(var h):
        var n = 0
        for(not n is this.m and rc is not None):# 
            print(rc.left,rc.right,rc.top,rc.bottom)
        var rect_is_live = if(m < 3) true elif(6 < m and m < 10) false else true
        this.rc = if(not rect_is_live) None else pass

    pub operator==(bool)(Myclass T): #演算子オーバーロード+,-,==,is,and,or,not,*,/,++,--,**,//,%,^,<<,>>,<=,>=,<,>,|,&,!(後ろ7つはビット演算子) 但し,as,=, :=,., ?:, ->, new,typeはオーバーロード不可
        #+=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=,!=は2項演算子をオーバーロードすれば可能
        return (if(not this.m is not T.m) false elif(this.rc is not T.rc) false elif(this.h is not T.h) false elif(this.y is not T.y) false elif(this.t is not T.t) false else true)

    set(val) get int m
    del opt ref rect rc
    var h
    var y
    ref var t

#条件つきジェネリクス(簡易ジェネリクスはvarで作成可能)
template<T>
def Tshow(T t):
    if(type(T) is int opt int or str or opt str or float or opt float):
        opt int s = if(T)
    else:
        err.comment("vakerror0001:この型でジェネリクスは作られていません")

def sum(var)(var t1,var t2):
    return t1 + t2 #コンパイル時にt1とt2の型が間違えていると読んでいるとコンパイル時エラーが出る。

#名前空間(基本は名前空間に書く今回は特別に外)namespaceとimport名を分けることにより日本語文字列の名前空間をできるだけ排除しやすいようにする。

namespace ex:
    main(opt int)():
        return Null 
        #名前空間内ex.main()でアクセス可能。(下まで読まれた後main関数が起動する(もし外部にあった場合コンパイル時にmain関数にinline化されmainの中に入る)。)

#アスペクト(共通アスペクト)
diversity MyLogging(asp):
    def log(int i):
        print(f"{i}です")

シールド言語について

よく似た言語にBoo言語がありますが、Python以上にC言語に似ていないため流行らなかったのだと思い、少しでもC,C++,C#風に書ける静的型付けでコンパイルしやすいPythonとして作ってみました。従って,C#やKotlinも参考にしました。次のはポインタあり版です。.shieでもいいですが、少し文法が違うので.shiexにしました。

ex.shiex
using system
using os
using rapt
using console
using err
@pragma out console #out console,only window,out window(デフォルトはout console)
@pragma style signal #C言語スタイルで書くことが可能。 
#主なポインタ
#thisptr->thisは参照型。thisptrはポインタ。(osに付属)
#ptr,o_ptr->*だと掛け算と見間違える可能性もあり可読性が低下するため(osに付属)
#os.id(name)

def unsafe main(opt int)(){
    var A[4] = Array<int>{1,2,3,4};
    opt int os.ptr s_p = os.id(A);
    print(os.o_ptr s_p);#output 1
    s_p++;
    int s = os.o_ptr s_p;
    s_p = os.nullptr;
    ref int AA = new {4,5,3,2};
    if(true){
        ref int BB = AA;
    }
    print(AA[2]); #コンパイルエラー
    print(BB[2]); #コンパイルエラー
    print(A[1]); #2
    print(s); #output 2
    return Null;#正常終了
}

誰か作れる技術のある方は是非とも作っていただきたい。書いて思ったことは、IDEがないと書きずらいな。


Viewing all articles
Browse latest Browse all 9743

Trending Articles