見出し画像

【その0・Nim入門】Nimで作るインタプリタを写経する 。

上の記事を発見したので、パクって写経してみようと思います。

Nimについておさらい

変数

早見表

サンプルコード

const a = "A"
var a = "A"
let a = "A"

構造体

早見表

サンプルコード

type Sample = object # これはスタックに格納される
  x: int
  y: string

type Sample = ref object # これはヒープに格納される
  x: int
  y: string

type Sample = ptr object # GCでこれは破棄されないようになります(注意)
  x: int
  y: string

type
  NilableSample = ref object
    x, y: string
  Sample = NilableA not nil  # これはNot Nil制約

type Sample = distinct int # 別名の型

type Direction = enum # 値=キー
  North
  East
  ...

type Direction = enum # 文字列の値
  North = "North"
  East  = "East"
  ...

type Direction = enum # 数字の値
  North = 1
  East  = 2
  ...

type Direction {.pure.} = enum #これは Direction.Nortch みたいに使ってほしい時
  North
  East
  ...

ファンクション(proc, method, func)

早見表

サンプルコード

type Sample = object of RootObj

proc call(sample: Sample): string = # 基本的にはこれ
  return "Hello" World]

method call2(self: Sample, a: int): void {.base.} = # オーバーライドするならこれ
    echo fmt"Sample: {a}"

func call3(self: Sample, a: int): string = # 副作用を許さない
    return fmt"Sample: {a}"


インターフェイス

インターフェイスについてはタプルで対応するそうです!


まとめ✅

ざっくりですが、nimの学習ポイントをまとめておきました。
その他はプログラミング言語経験があればわかるかなぁってのが多かったので、今回はこの程度で許してください🙇‍♂️
ではインタプリタ作っていこうと思います。


参考

https://flat-leon.hatenablog.com/entry/nim_howto


この記事が気に入ったらサポートをしてみませんか?