2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

自作プログラミング言語の設計!2(構文の定義)

2
Posted at

まえがき

前回の続きから始めます...

ループ

ターゲットを考慮するといわゆるwhileループ、forループはわかりにくいのではないかと思います
-> 回数指定、回数指定なしのループを実装すれば使いやすくなるのではないでしょうか...

関数

ほとんど既存の言語と一致してしまうと思います

ラムダ式は便利ですが実装コストと前に決定した型の問題もあるのでいったん保留します

ただし、「宣言ー>使う」ということを重視したいので変数と同様でコードの順番として宣言していない関数は使えないという仕様を検討しています

->実装なし関数の宣言(のちに実装が必要)を許容してもいいかも...

ブロック

条件分岐やループ、関数でブロックの開始はそれぞれのキーワードでよいと思います(既存の言語とほとんど変わりません)

ブロックの閉じが既存の言語ではある記号(開始にも使用)を使う場合とインデントで示す場合があります
前者はエラーを起こしにくいです
後者は視覚性が富んでいますが、ずれるとエラーが出たりします

-> 閉じのキーワードを用意してインデントを任意にしたいと思います(ターゲットを考えてもわかりやすくなると思います)

命令の区切り

既存の言語にもある、改行で区切る方式にします(視覚性の向上にもつながるかも..)

おおまかな構文

簡易的に構文を書いてきました!

program = ((def_decl | function_decl | statement) NewLine)* EOF

def_decl
    = "def" type identifier "=" literal
    | "def" "function" identifier "(" param_list? ")" ":" type

function_decl
    = "function" identifier "(" param_list? ")" ":" type
        (statement NewLine)*?
      "end" ("function")?

statement
    = var_decl
    | assign_stmt
    | expr_stmt
    | loop_stmt
    | if_stmt
    | continue_stmt
    | break_stmt
    | return_stmt

var_decl
    = "var" identifier "=" expression
    | type identifier "=" expression
    | type identifier

assign_stmt
    = identifier "=" expression
    | identifier "+=" expression
    | identifier "-=" expression
    | identifier "*=" expression
    | identifier "/=" expression
    | identifier "%=" expression
    | identifier "&=" expression
    | identifier "|=" expression

expr_stmt = expression | (identifier "(" (expression)*? ")")

expression = logic_expr

logic_expr = add_expr (("is" | "==" | "!=" | ">=" | "<=" | ">" | "<") add_expr)?

add_expr = mul_expr (("+" | "-") mul_expr)*

mul_expr = unary_expr (("*" | "/" | "%") unary_expr)*

unary_expr = primary_expr

primary_expr
    = literal
    | identifier
    | identifier "(" arg_list? ")"
    | identifier "[" expression "]"
    | "(" expression ")"

literal
    = number_literal
    | decimal_literal
    | string_literal
    | boolean_literal
    | type_literal
    | char_literal
    | char_array_literal
    | number_array_literal
    | decimal_array_literal
    | boolean_array_literal
    | type_array_literal

loop_stmt
    = "loop" "(" expression ")" statement* "end" ("loop")?
    | "loop" statement* "end" ("loop")?

if_stmt
    = "if" "(" expression ")" statement*
      ("elseif" "(" expression ")" statement*)*
      ("else" statement*)?
      "end" ("if")?

continue_stmt = "continue"
break_stmt    = "break"
return_stmt   = "return" expression?

?

おわり

今回はこれまでにします
続きは ここ から!

ありがとうございました

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?