たなしょのメモ

日々勉強していることをつらつらと

OS作成奮闘記day44

本では30日目になりました。
Makefile

APP      = calc
STACK    = 4k
MALLOC   = 0k

include ../app_make.txt

calc.c

#include "apilib.h"
#include <stdio.h>       /* sprintf */

#define INVALID        -0x7fffffff

int strtol(char *s, char **endp, int base); /* 標準関数(stdlib.h) */

char *skipspace(char *p);
int getnum(char **pp, int priority);

void HariMain(void)
{
    int i;
    char s[30], *p;

    api_cmdline(s, 30);
    for (p = s; *p > ' '; p++) { }    /* スペースが来るまで読み飛ばす */
    i = getnum(&p, 9);
    if (i == INVALID) {
        api_putstr0("error!\n");
    } else {
        sprintf(s, "= %d = 0x%x\n", i, i);
        api_putstr0(s);
    }
    api_end();
}

char *skipspace(char *p)
{
    for (; *p == ' '; p++) { }   /* スペースを読み飛ばす */
    return p;
}

int getnum(char **pp, int priority)
{
    char *p = *pp;
    int i = INVALID, j;
    p = skipspace(p);

    /* 単項演算子 */
    if (*p == '+') {
        p = skipspace(p + 1);
        i = getnum(&p, 0);
    } else if (*p == '-') {
        p = skipspace(p + 1);
        i = getnum(&p, 0);
        if (i != INVALID) {
            i = - i;
        }
    } else if (*p == '~') {
        p = skipspace(p + 1);
        i = getnum(&p, 0);
        if (i != INVALID) {
            i = ~i;
        }
    } else if (*p == '(') {    /* かっこ */
        p = skipspace(p + 1);
        i = getnum(&p, 9);
        if (*p == ')') {
            p = skipspace(p + 1);
        } else {
            i = INVALID;
        }
    } else if ('0' <= *p && *p <= '9') { /* 数値 */
        i = strtol(p, &p, 0);
    } else { /* エラー */
        i = INVALID;
    }

    /* 二項演算子 */
    for (;;) {
        if (i == INVALID) {
            break;
        }
        p = skipspace(p);
        if (*p == '+' && priority > 2) {
            p = skipspace(p + 1);
            j = getnum(&p, 2);
            if (j != INVALID) {
                i += j;
            } else {
                i = INVALID;
            }
        } else if (*p == '-' && priority > 2) {
            p = skipspace(p + 1);
            j = getnum(&p, 2);
            if (j != INVALID) {
                i -= j;
            } else {
                i = INVALID;
            }
        } else if (*p == '*' && priority > 1) {
            p = skipspace(p + 1);
            j = getnum(&p, 1);
            if (j != INVALID) {
                i *= j;
            } else {
                i = INVALID;
            }
        } else if (*p == '/' && priority > 1) {
            p = skipspace(p + 1);
            j = getnum(&p, 1);
            if (j != INVALID && j != 0) {
                i /= j;
            } else {
                i = INVALID;
            }
        } else if (*p == '%' && priority > 1) {
            p = skipspace(p + 1);
            j = getnum(&p, 1);
            if (j != INVALID && j != 0) {
                i %= j;
            } else {
                i = INVALID;
            }
        } else if (*p == '<' && p[1] == '<' && priority > 3) {
            p = skipspace(p + 2);
            j = getnum(&p, 3);
            if (j != INVALID && j != 0) {
                i <<= j;
            } else {
                i = INVALID;
            }
        } else if (*p == '>' && p[1] == '>' && priority > 3) {
            p = skipspace(p + 2);
            j = getnum(&p, 3);
            if (j != INVALID && j != 0) {
                i >>= j;
            } else {
                i = INVALID;
            }
        } else if (*p == '&' && priority > 4) {
            p = skipspace(p + 1);
            j = getnum(&p, 4);
            if (j != INVALID) {
                i &= j;
            } else {
                i = INVALID;
            }
        } else if (*p == '^' && priority > 5) {
            p = skipspace(p + 1);
            j = getnum(&p, 5);
            if (j != INVALID) {
                i ^= j;
            } else {
                i = INVALID;
            }
        } else if (*p == '|' && priority > 6) {
            p = skipspace(p + 1);
            j = getnum(&p, 6);
            if (j != INVALID) {
                i |= j;
            } else {
                i = INVALID;
            }
        } else {
            break;
        }
    }
    p = skipspace(p);
    *pp = p;
    return i;
}

Makefile

TOOLPATH = ..\..\z_tools\\

INCPATH  = ..\..\z_tools\haribote\\

MAKE     = $(TOOLPATH)make.exe -r
EDIMG    = $(TOOLPATH)edimg.exe
IMGTOL   = $(TOOLPATH)imgtol.com
COPY     = cmd.exe /C copy
DEL      = rm -f

# default operation

default :
    $(MAKE) haribote.img

# file making rule

haribote.img : haribote/ipl20.bin haribote/haribote.sys Makefile \
        a/a.hrb hello3/hello3.hrb hello4/hello4.hrb hello5/hello5.hrb \
        winhelo/winhelo.hrb winhelo2/winhelo2.hrb winhelo3/winhelo3.hrb \
        star1/star1.hrb stars/stars.hrb stars2/stars2.hrb \
        lines/lines.hrb walk/walk.hrb noodle/noodle.hrb \
        beepdown/beepdown.hrb color/color.hrb color2/color2.hrb \
        sosu/sosu.hrb sosu2/sosu2.hrb sosu3/sosu3.hrb \
        type/type.hrb iroha/iroha.hrb chklang/chklang.hrb \
        notrec/notrec.hrb bball/bball.hrb invader/invader.hrb \
        calc/calc.hrb
    $(EDIMG)   imgin:../../z_tools/fdimg0at.tek \
        wbinimg src:haribote/ipl20.bin len:512 from:0 to:0 \
        copy from:haribote/haribote.sys to:@: \
        copy from:haribote/ipl20.nas to:@: \
        copy from:make.bat to:@: \
        copy from:a/a.hrb to:@: \
        copy from:hello3/hello3.hrb to:@: \
        copy from:hello4/hello4.hrb to:@: \
        copy from:hello5/hello5.hrb to:@: \
        copy from:winhelo/winhelo.hrb to:@: \
        copy from:winhelo2/winhelo2.hrb to:@: \
        copy from:winhelo3/winhelo3.hrb to:@: \
        copy from:star1/star1.hrb to:@: \
        copy from:stars/stars.hrb to:@: \
        copy from:stars2/stars2.hrb to:@: \
        copy from:lines/lines.hrb to:@: \
        copy from:walk/walk.hrb to:@: \
        copy from:noodle/noodle.hrb to:@: \
        copy from:beepdown/beepdown.hrb to:@: \
        copy from:color/color.hrb to:@: \
        copy from:color2/color2.hrb to:@: \
        copy from:sosu/sosu.hrb to:@: \
        copy from:sosu2/sosu2.hrb to:@: \
        copy from:sosu3/sosu3.hrb to:@: \
        copy from:type/type.hrb to:@: \
        copy from:iroha/iroha.hrb to:@: \
        copy from:chklang/chklang.hrb to:@: \
        copy from:euc.txt to:@: \
        copy from:notrec/notrec.hrb to:@: \
        copy from:bball/bball.hrb to:@: \
        copy from:invader/invader.hrb to:@: \
        copy from:calc/calc.hrb to:@: \
        copy from:nihongo/nihongo.fnt to:@: \
        imgout:haribote.img

# command

run :
    $(MAKE) haribote.img
    $(COPY) haribote.img ..\..\z_tools\qemu\fdimage0.bin
    $(MAKE) -C ..\..\z_tools\qemu

install :
    $(MAKE) haribote.img
    $(IMGTOL) w a: haribote.img

full :
    $(MAKE) -C haribote
    $(MAKE) -C apilib
    $(MAKE) -C a
    $(MAKE) -C hello3
    $(MAKE) -C hello4
    $(MAKE) -C hello5
    $(MAKE) -C winhelo
    $(MAKE) -C winhelo2
    $(MAKE) -C winhelo3
    $(MAKE) -C star1
    $(MAKE) -C stars
    $(MAKE) -C stars2
    $(MAKE) -C lines
    $(MAKE) -C walk
    $(MAKE) -C noodle
    $(MAKE) -C beepdown
    $(MAKE) -C color
    $(MAKE) -C color2
    $(MAKE) -C sosu
    $(MAKE) -C sosu2
    $(MAKE) -C sosu3
    $(MAKE) -C type
    $(MAKE) -C iroha
    $(MAKE) -C chklang
    $(MAKE) -C notrec
    $(MAKE) -C bball
    $(MAKE) -C invader
    $(MAKE) -C calc
    $(MAKE) haribote.img

run_full :
    $(MAKE) full
    $(COPY) haribote.img ..\..\z_tools\qemu\fdimage0.bin
    $(MAKE) -C ..\..\z_tools\qemu

install_full :
    $(MAKE) full
    $(IMGTOL) w a: haribote.img

run_os :
    $(MAKE) -C haribote
    $(MAKE) run

clean :
# 何もしない

src_only :
    $(MAKE) clean
    -$(DEL) haribote.img
    
clean_full :
    $(MAKE) -C haribote     clean
    $(MAKE) -C apilib       clean
    $(MAKE) -C a            clean
    $(MAKE) -C hello3       clean
    $(MAKE) -C hello4       clean
    $(MAKE) -C hello5       clean
    $(MAKE) -C winhelo      clean
    $(MAKE) -C winhelo2     clean
    $(MAKE) -C winhelo3     clean
    $(MAKE) -C star1        clean
    $(MAKE) -C stars        clean
    $(MAKE) -C stars2       clean
    $(MAKE) -C lines        clean
    $(MAKE) -C walk         clean   
    $(MAKE) -C noodle       clean
    $(MAKE) -C beepdown     clean
    $(MAKE) -C color        clean
    $(MAKE) -C color2       clean
    $(MAKE) -C sosu         clean
    $(MAKE) -C sosu2        clean
    $(MAKE) -C sosu3        clean
    $(MAKE) -C type         clean
    $(MAKE) -C iroha        clean
    $(MAKE) -C chklang      clean
    $(MAKE) -C notrec       clean
    $(MAKE) -C bball        clean
    $(MAKE) -C invader      clean
    $(MAKE) -C calc         clean

src_only_full :
    $(MAKE) -C haribote     src_only
    $(MAKE) -C apilib       src_only
    $(MAKE) -C a            src_only
    $(MAKE) -C hello3       src_only
    $(MAKE) -C hello4       src_only
    $(MAKE) -C hello5       src_only
    $(MAKE) -C winhelo      src_only
    $(MAKE) -C winhelo2     src_only
    $(MAKE) -C winhelo3     src_only
    $(MAKE) -C star1        src_only
    $(MAKE) -C stars        src_only
    $(MAKE) -C stars2       src_only
    $(MAKE) -C lines        src_only
    $(MAKE) -C walk         src_only    
    $(MAKE) -C noodle       src_only
    $(MAKE) -C beepdown     src_only
    $(MAKE) -C color        src_only
    $(MAKE) -C color2       src_only
    $(MAKE) -C sosu         src_only
    $(MAKE) -C sosu2        src_only
    $(MAKE) -C sosu3        src_only
    $(MAKE) -C type         src_only
    $(MAKE) -C iroha        src_only
    $(MAKE) -C chklang      src_only
    $(MAKE) -C notrec       src_only
    $(MAKE) -C bball        src_only
    $(MAKE) -C invader      src_only
    $(MAKE) -C calc         src_only
    -$(DEL) haribote.img

refresh :
    $(MAKE) full
    $(MAKE) clean_full
    -$(DEL) haribote.img

実装過程
・ ed1a180f460ea7d79e22493d861fc63e001ab626