Skip to content

Commit

Permalink
subSeq, and, or, xor関数を追加
Browse files Browse the repository at this point in the history
  • Loading branch information
michirakara committed Apr 20, 2024
1 parent 82b7532 commit 27d0742
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 2 deletions.
10 changes: 9 additions & 1 deletion docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ rhsが等しいか判定する
conditionが真ならiftrue(元の値)、偽ならiffalse(元の値)を返す
## set(idx, value)
元の配列のidx番目をvalueに変えた配列を返す
## subSeq(start,end)
文字列または配列LのL[start,end)を返します
## and(rhs)
intまたはboolのandをする
## or(rhs)
intまたはboolのorをする
## xor(rhs)
intまたはboolのxorをする

# \<init>のところのやつ一覧
## nextIn<>
Expand All @@ -62,4 +70,4 @@ valueで初期化された長さsizeのリストを返す
## FUNC<arg1, arg2, ... , func>
ラムダ式。arg1, arg2, ... を引数としてfuncを実行する関数を返す
## それ以外
雑にPythonでevalしているのでなんでもできます(ダメ)
文字列、int、floatの定数が使えます
5 changes: 5 additions & 0 deletions examples/abc350-a.mcn
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
num = nextIn ~subSeq(3, 6) ~parseInt();
_ = num ~ifElse(num ~gt(0) ~and(num ~lt(350)) ~and(num ~eq(316) ~flip()),
FUNC<i, "Yes" ~println()>,
FUNC<i, "No" ~println()>,
);
4 changes: 4 additions & 0 deletions lib/func_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
"iota",
"ifElse",
"set",
"subSeq",
"and",
"or",
"xor",
]


Expand Down
2 changes: 1 addition & 1 deletion lib/parser_.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,6 @@ def parse_expression(expression: str) -> Expression:
chains.append(parse_method(expression[start_idx:idx].strip()))
start_idx = idx + 1
idx += 1
if expression[start_idx:].strip()!="":
if expression[start_idx:].strip() != "":
chains.append(parse_method(expression[start_idx:].strip()))
return Expression(head, chains)
24 changes: 24 additions & 0 deletions lib/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,14 @@ def run_method(val: Value, method: Method) -> Value:
return ifElse_(val, *args)
case "set":
return set_(val, *args)
case "subSeq":
return subSeq_(val, *args)
case "and":
return and_(val, *args)
case "or":
return or_(val, *args)
case "xor":
return xor_(val, *args)


def add_(lhs: Value, to_add: Value) -> Value:
Expand Down Expand Up @@ -217,3 +225,19 @@ def set_(lhs: Value, idx: Value, value: Value) -> Value:
ret = deepcopy(lhs).val
ret[idx.val] = value
return Value(ret)


def subSeq_(lhs: Value, start: Value, end: Value) -> Value:
return Value(deepcopy(lhs).val[start.val : end.val])


def and_(lhs: Value, rhs: Value) -> Value:
return Value(lhs.val & rhs.val)


def or_(lhs: Value, rhs: Value) -> Value:
return Value(lhs.val | rhs.val)


def xor_(lhs: Value, rhs: Value) -> Value:
return Value(lhs.val ^ rhs.val)

0 comments on commit 27d0742

Please sign in to comment.