Add a utility for generating Rust 'match' expressions.
This makes it a little simpler to generate 'match' statements, and
it performs deduplication of identical arms. And it means I don't
have to think about as many strings like '{} {{ {}.. }} => {}'
when I'm trying to think about how instructions work :-).
This commit is contained in:
@@ -49,11 +49,11 @@ def gen_formats(fmt):
|
|||||||
with fmt.indented(
|
with fmt.indented(
|
||||||
"fn from(inst: &'a InstructionData) -> InstructionFormat {",
|
"fn from(inst: &'a InstructionData) -> InstructionFormat {",
|
||||||
'}'):
|
'}'):
|
||||||
with fmt.indented('match *inst {', '}'):
|
m = srcgen.Match('*inst')
|
||||||
for f in InstructionFormat.all_formats:
|
for f in InstructionFormat.all_formats:
|
||||||
fmt.line(('InstructionData::{} {{ .. }} => ' +
|
m.arm('InstructionData::' + f.name, ['..'],
|
||||||
'InstructionFormat::{},')
|
'InstructionFormat::' + f.name)
|
||||||
.format(f.name, f.name))
|
fmt.match(m)
|
||||||
fmt.line()
|
fmt.line()
|
||||||
|
|
||||||
|
|
||||||
@@ -74,33 +74,32 @@ def gen_arguments_method(fmt, is_mut):
|
|||||||
'pool: &\'a {m}ir::ValueListPool) -> '
|
'pool: &\'a {m}ir::ValueListPool) -> '
|
||||||
'&{m}[Value] {{'
|
'&{m}[Value] {{'
|
||||||
.format(f=method, m=mut), '}'):
|
.format(f=method, m=mut), '}'):
|
||||||
with fmt.indented('match *self {', '}'):
|
m = srcgen.Match('*self')
|
||||||
for f in InstructionFormat.all_formats:
|
for f in InstructionFormat.all_formats:
|
||||||
n = 'InstructionData::' + f.name
|
n = 'InstructionData::' + f.name
|
||||||
|
|
||||||
# Formats with a value list put all of their arguments in the
|
# Formats with a value list put all of their arguments in the
|
||||||
# list. We don't split them up, just return it all as variable
|
# list. We don't split them up, just return it all as variable
|
||||||
# arguments. (I expect the distinction to go away).
|
# arguments. (I expect the distinction to go away).
|
||||||
if f.has_value_list:
|
if f.has_value_list:
|
||||||
arg = ''.format(mut)
|
m.arm(n, ['ref {}args'.format(mut), '..'],
|
||||||
fmt.line(
|
'args.{}(pool)'.format(as_slice))
|
||||||
'{} {{ ref {}args, .. }} => args.{}(pool),'
|
continue
|
||||||
.format(n, mut, as_slice))
|
|
||||||
continue
|
|
||||||
|
|
||||||
# Fixed args.
|
# Fixed args.
|
||||||
if f.num_value_operands == 0:
|
fields = []
|
||||||
arg = '&{}[]'.format(mut)
|
if f.num_value_operands == 0:
|
||||||
capture = ''
|
arg = '&{}[]'.format(mut)
|
||||||
elif f.num_value_operands == 1:
|
elif f.num_value_operands == 1:
|
||||||
capture = 'ref {}arg, '.format(mut)
|
fields.append('ref {}arg'.format(mut))
|
||||||
arg = '{}(arg)'.format(rslice)
|
arg = '{}(arg)'.format(rslice)
|
||||||
else:
|
else:
|
||||||
capture = 'ref {}args, '.format(mut)
|
args = 'args_arity{}'.format(f.num_value_operands)
|
||||||
arg = 'args'
|
fields.append('args: ref {}{}'.format(mut, args))
|
||||||
fmt.line(
|
arg = args
|
||||||
'{} {{ {}.. }} => {},'
|
fields.append('..')
|
||||||
.format(n, capture, arg))
|
m.arm(n, fields, arg)
|
||||||
|
fmt.match(m)
|
||||||
|
|
||||||
|
|
||||||
def gen_instruction_data(fmt):
|
def gen_instruction_data(fmt):
|
||||||
@@ -155,39 +154,37 @@ def gen_instruction_data_impl(fmt):
|
|||||||
with fmt.indented('impl InstructionData {', '}'):
|
with fmt.indented('impl InstructionData {', '}'):
|
||||||
fmt.doc_comment('Get the opcode of this instruction.')
|
fmt.doc_comment('Get the opcode of this instruction.')
|
||||||
with fmt.indented('pub fn opcode(&self) -> Opcode {', '}'):
|
with fmt.indented('pub fn opcode(&self) -> Opcode {', '}'):
|
||||||
with fmt.indented('match *self {', '}'):
|
m = srcgen.Match('*self')
|
||||||
for f in InstructionFormat.all_formats:
|
for f in InstructionFormat.all_formats:
|
||||||
fmt.line(
|
m.arm('InstructionData::' + f.name, ['opcode', '..'],
|
||||||
'InstructionData::{} {{ opcode, .. }} => opcode,'
|
'opcode')
|
||||||
.format(f.name))
|
fmt.match(m)
|
||||||
fmt.line()
|
fmt.line()
|
||||||
|
|
||||||
fmt.doc_comment('Get the controlling type variable operand.')
|
fmt.doc_comment('Get the controlling type variable operand.')
|
||||||
with fmt.indented(
|
with fmt.indented(
|
||||||
'pub fn typevar_operand(&self, pool: &ir::ValueListPool) -> '
|
'pub fn typevar_operand(&self, pool: &ir::ValueListPool) -> '
|
||||||
'Option<Value> {', '}'):
|
'Option<Value> {', '}'):
|
||||||
with fmt.indented('match *self {', '}'):
|
m = srcgen.Match('*self')
|
||||||
for f in InstructionFormat.all_formats:
|
for f in InstructionFormat.all_formats:
|
||||||
n = 'InstructionData::' + f.name
|
n = 'InstructionData::' + f.name
|
||||||
if f.typevar_operand is None:
|
if f.typevar_operand is None:
|
||||||
fmt.line(n + ' { .. } => None,')
|
m.arm(n, ['..'], 'None')
|
||||||
elif f.has_value_list:
|
elif f.has_value_list:
|
||||||
# We keep all arguments in a value list.
|
# We keep all arguments in a value list.
|
||||||
i = f.typevar_operand
|
i = f.typevar_operand
|
||||||
fmt.line(
|
m.arm(n, ['ref args', '..'],
|
||||||
'{} {{ ref args, .. }} => '
|
'args.get({}, pool)'.format(i))
|
||||||
'args.get({}, pool),'.format(n, i))
|
elif f.num_value_operands == 1:
|
||||||
elif f.num_value_operands == 1:
|
# We have a single value operand called 'arg'.
|
||||||
# We have a single value operand called 'arg'.
|
m.arm(n, ['arg', '..'], 'Some(arg)')
|
||||||
fmt.line(n + ' { arg, .. } => Some(arg),')
|
else:
|
||||||
else:
|
# We have multiple value operands and an array `args`.
|
||||||
# We have multiple value operands and an array `args`.
|
# Which `args` index to use?
|
||||||
# Which `args` index to use?
|
args = 'args_arity{}'.format(f.num_value_operands)
|
||||||
i = f.typevar_operand
|
m.arm(n, ['args: ref {}'.format(args), '..'],
|
||||||
fmt.line(
|
'Some({}[{}])'.format(args, f.typevar_operand))
|
||||||
n +
|
fmt.match(m)
|
||||||
' {{ ref args, .. }} => Some(args[{}]),'
|
|
||||||
.format(i))
|
|
||||||
fmt.line()
|
fmt.line()
|
||||||
|
|
||||||
fmt.doc_comment(
|
fmt.doc_comment(
|
||||||
@@ -216,13 +213,13 @@ def gen_instruction_data_impl(fmt):
|
|||||||
with fmt.indented(
|
with fmt.indented(
|
||||||
'pub fn take_value_list(&mut self) -> Option<ir::ValueList> {',
|
'pub fn take_value_list(&mut self) -> Option<ir::ValueList> {',
|
||||||
'}'):
|
'}'):
|
||||||
with fmt.indented('match *self {', '}'):
|
m = srcgen.Match('*self')
|
||||||
for f in InstructionFormat.all_formats:
|
for f in InstructionFormat.all_formats:
|
||||||
n = 'InstructionData::' + f.name
|
n = 'InstructionData::' + f.name
|
||||||
if f.has_value_list:
|
if f.has_value_list:
|
||||||
fmt.line(
|
m.arm(n, ['ref mut args', '..'], 'Some(args.take())')
|
||||||
n + ' { ref mut args, .. } => Some(args.take()),')
|
m.arm('_', [], 'None')
|
||||||
fmt.line('_ => None,')
|
fmt.match(m)
|
||||||
fmt.line()
|
fmt.line()
|
||||||
|
|
||||||
fmt.doc_comment(
|
fmt.doc_comment(
|
||||||
@@ -307,14 +304,12 @@ def gen_opcodes(groups, fmt):
|
|||||||
fmt.doc_comment(Instruction.ATTRIBS[attr])
|
fmt.doc_comment(Instruction.ATTRIBS[attr])
|
||||||
with fmt.indented('pub fn {}(self) -> bool {{'
|
with fmt.indented('pub fn {}(self) -> bool {{'
|
||||||
.format(attr), '}'):
|
.format(attr), '}'):
|
||||||
with fmt.indented('match self {', '}'):
|
m = srcgen.Match('self')
|
||||||
for i in instrs:
|
for i in instrs:
|
||||||
if getattr(i, attr):
|
if getattr(i, attr):
|
||||||
fmt.format(
|
m.arm('Opcode::' + i.camel_name, [], 'true')
|
||||||
'Opcode::{} => true,',
|
m.arm('_', [], 'false')
|
||||||
i.camel_name, i.name)
|
fmt.match(m)
|
||||||
|
|
||||||
fmt.line('_ => false,')
|
|
||||||
fmt.line()
|
fmt.line()
|
||||||
fmt.line()
|
fmt.line()
|
||||||
|
|
||||||
@@ -331,9 +326,10 @@ def gen_opcodes(groups, fmt):
|
|||||||
|
|
||||||
# Generate a private opcode_name function.
|
# Generate a private opcode_name function.
|
||||||
with fmt.indented('fn opcode_name(opc: Opcode) -> &\'static str {', '}'):
|
with fmt.indented('fn opcode_name(opc: Opcode) -> &\'static str {', '}'):
|
||||||
with fmt.indented('match opc {', '}'):
|
m = srcgen.Match('opc')
|
||||||
for i in instrs:
|
for i in instrs:
|
||||||
fmt.format('Opcode::{} => "{}",', i.camel_name, i.name)
|
m.arm('Opcode::' + i.camel_name, [], '"{}"'.format(i.name))
|
||||||
|
fmt.match(m)
|
||||||
fmt.line()
|
fmt.line()
|
||||||
|
|
||||||
# Generate an opcode hash table for looking up opcodes by name.
|
# Generate an opcode hash table for looking up opcodes by name.
|
||||||
|
|||||||
@@ -57,12 +57,11 @@ def gen_getter(setting, sgrp, fmt):
|
|||||||
ty = camel_case(setting.name)
|
ty = camel_case(setting.name)
|
||||||
proto = 'pub fn {}(&self) -> {}'.format(setting.name, ty)
|
proto = 'pub fn {}(&self) -> {}'.format(setting.name, ty)
|
||||||
with fmt.indented(proto + ' {', '}'):
|
with fmt.indented(proto + ' {', '}'):
|
||||||
with fmt.indented(
|
m = srcgen.Match('self.bytes[{}]'.format(setting.byte_offset))
|
||||||
'match self.bytes[{}] {{'
|
for i, v in enumerate(setting.values):
|
||||||
.format(setting.byte_offset), '}'):
|
m.arm(str(i), [], '{}::{}'.format(ty, camel_case(v)))
|
||||||
for i, v in enumerate(setting.values):
|
m.arm('_', [], 'panic!("Invalid enum value")')
|
||||||
fmt.line('{} => {}::{},'.format(i, ty, camel_case(v)))
|
fmt.match(m)
|
||||||
fmt.line('_ => panic!("Invalid enum value"),')
|
|
||||||
else:
|
else:
|
||||||
raise AssertionError("Unknown setting kind")
|
raise AssertionError("Unknown setting kind")
|
||||||
|
|
||||||
|
|||||||
@@ -8,9 +8,10 @@ source code.
|
|||||||
from __future__ import absolute_import
|
from __future__ import absolute_import
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
|
import collections
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from typing import Any, List # noqa
|
from typing import Any, List, Set, Tuple # noqa
|
||||||
except ImportError:
|
except ImportError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@@ -146,6 +147,52 @@ class Formatter(object):
|
|||||||
for l in parse_multiline(s):
|
for l in parse_multiline(s):
|
||||||
self.line('/// ' + l if l else '///')
|
self.line('/// ' + l if l else '///')
|
||||||
|
|
||||||
|
def match(self, m):
|
||||||
|
# type: (Match) -> None
|
||||||
|
"""
|
||||||
|
Add a match expression.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
>>> f = Formatter()
|
||||||
|
>>> m = Match('x')
|
||||||
|
>>> m.arm('Orange', ['a', 'b'], 'some body')
|
||||||
|
>>> m.arm('Yellow', ['a', 'b'], 'some body')
|
||||||
|
>>> m.arm('Green', ['a', 'b'], 'different body')
|
||||||
|
>>> m.arm('Blue', ['x', 'y'], 'some body')
|
||||||
|
>>> f.match(m)
|
||||||
|
>>> f.writelines()
|
||||||
|
match x {
|
||||||
|
Orange { a, b } |
|
||||||
|
Yellow { a, b } => {
|
||||||
|
some body
|
||||||
|
}
|
||||||
|
Green { a, b } => {
|
||||||
|
different body
|
||||||
|
}
|
||||||
|
Blue { x, y } => {
|
||||||
|
some body
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
"""
|
||||||
|
with self.indented('match {} {{'.format(m.expr), '}'):
|
||||||
|
for (fields, body), names in m.arms.items():
|
||||||
|
with self.indented('', '}'):
|
||||||
|
names_left = len(names)
|
||||||
|
for name in names.keys():
|
||||||
|
fields_str = ', '.join(fields)
|
||||||
|
if len(fields) != 0:
|
||||||
|
fields_str = '{{ {} }} '.format(fields_str)
|
||||||
|
names_left -= 1
|
||||||
|
if names_left > 0:
|
||||||
|
suffix = '|'
|
||||||
|
else:
|
||||||
|
suffix = '=> {'
|
||||||
|
self.outdented_line(name + ' ' + fields_str + suffix)
|
||||||
|
if names_left == 0:
|
||||||
|
self.multi_line(body)
|
||||||
|
|
||||||
|
|
||||||
def _indent(s):
|
def _indent(s):
|
||||||
# type: (str) -> int
|
# type: (str) -> int
|
||||||
@@ -195,3 +242,36 @@ def parse_multiline(s):
|
|||||||
while trimmed and not trimmed[0]:
|
while trimmed and not trimmed[0]:
|
||||||
trimmed.pop(0)
|
trimmed.pop(0)
|
||||||
return trimmed
|
return trimmed
|
||||||
|
|
||||||
|
|
||||||
|
class Match(object):
|
||||||
|
"""
|
||||||
|
Match formatting class.
|
||||||
|
|
||||||
|
Match objects collect all the information needed to emit a Rust `match`
|
||||||
|
expression, automatically deduplicating overlapping identical arms.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
>>> m = Match('x')
|
||||||
|
>>> m.arm('Orange', ['a', 'b'], 'some body')
|
||||||
|
>>> m.arm('Yellow', ['a', 'b'], 'some body')
|
||||||
|
>>> m.arm('Green', ['a', 'b'], 'different body')
|
||||||
|
>>> m.arm('Blue', ['x', 'y'], 'some body')
|
||||||
|
>>> assert(len(m.arms) == 3)
|
||||||
|
|
||||||
|
Note that this class is ignorant of Rust types, and considers two fields
|
||||||
|
with the same name to be equivalent.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, expr):
|
||||||
|
# type: (str) -> None
|
||||||
|
self.expr = expr
|
||||||
|
self.arms = collections.OrderedDict() # type: collections.OrderedDict[Tuple[Tuple[str, ...], str], collections.OrderedDict[str, None]] # noqa
|
||||||
|
|
||||||
|
def arm(self, name, fields, body):
|
||||||
|
# type: (str, List[str], str) -> None
|
||||||
|
key = (tuple(fields), body)
|
||||||
|
if key not in self.arms:
|
||||||
|
self.arms[key] = collections.OrderedDict()
|
||||||
|
self.arms[key][name] = None
|
||||||
|
|||||||
Reference in New Issue
Block a user